<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Threadv</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta property="og:type" content="website">
<meta property="og:title" content="Threadv">
<meta property="og:url" content="http://yoursite.com/page/5/index.html">
<meta property="og:site_name" content="Threadv">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Threadv">
  
    <link rel="alternate" href="/atom.xml" title="Threadv" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Threadv</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Suche"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main">
  
    <article id="post-build.bat" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/12/24/build.bat/" class="article-date">
  <time datetime="2018-12-24T07:01:26.000Z" itemprop="datePublished">2018-12-24</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/12/24/build.bat/">build.bat</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="build-bat"><a href="#build-bat" class="headerlink" title="build.bat"></a>build.bat</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">::1 本地已经安装maven，svn</span><br><span class="line">set project_name=&quot;storybookfenxiao&quot;</span><br><span class="line">set ver=&quot;1.0.0&quot;</span><br><span class="line"></span><br><span class="line">set project_path=f:\tmp\storybook_fenxiao</span><br><span class="line">::第一次需要运行co</span><br><span class="line">svn co svn://121.xx.xx.xxx/xxx/storybook/fenxiao/fenxiao %project_path%</span><br><span class="line">::以后每次运行update</span><br><span class="line">svn update %project_path%</span><br><span class="line"></span><br><span class="line">rd /S /Q %project_name%</span><br><span class="line">mkdir %project_name%</span><br><span class="line">xcopy %project_path% %project_name% /s /e</span><br><span class="line">xcopy /y %project_name%\src\main\resources\prod %project_name%\src\main\resources /s /e</span><br><span class="line">echo &quot;请确认svn更新是否完成&quot;</span><br><span class="line">pause</span><br><span class="line"></span><br><span class="line">::maven package </span><br><span class="line">call mvn clean -f %project_name%</span><br><span class="line">call mvn package -f %project_name% -Dmaven.test.skip=true</span><br><span class="line">::del sale.jar</span><br><span class="line">echo &quot;try copy jar&quot;</span><br><span class="line">copy /y %project_name%\target\fenxiao-0.0.1-SNAPSHOT.jar %cd%\fenxiao.jar</span><br><span class="line"></span><br><span class="line">echo &quot;请确认build是否完成&quot;</span><br><span class="line">pause</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">::build</span><br><span class="line">docker build . -t %project_name%:%ver%</span><br><span class="line"></span><br><span class="line">echo &quot;请确认docker是否完成&quot;</span><br><span class="line">pause</span><br><span class="line"></span><br><span class="line">::4 提交镜像</span><br><span class="line">docker tag %project_name%:%ver% registry.cn-hangzhou.aliyuncs.com/com_ifenghui/%project_name%:%ver%</span><br><span class="line">docker push registry.cn-hangzhou.aliyuncs.com/com_ifenghui/%project_name%:%ver%</span><br><span class="line">pause</span><br><span class="line"></span><br><span class="line">echo &quot;success!&quot;</span><br><span class="line">pause</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/12/24/build.bat/" data-id="ckcc5iwjy001iq4uwvh9usti2" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/docker/">docker</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-start-beta.sh" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/12/24/start-beta.sh/" class="article-date">
  <time datetime="2018-12-24T07:01:26.000Z" itemprop="datePublished">2018-12-24</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/12/24/start-beta.sh/">start-beta.sh</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="start-beta-sh"><a href="#start-beta-sh" class="headerlink" title="start-beta.sh"></a>start-beta.sh</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar /opt/storybookfenxiao/fenxiao.jar --spring.profiles.active=beta</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/12/24/start-beta.sh/" data-id="ckcc5iwle0036q4uw7iloq8gj" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/docker/">docker</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-start-product.sh" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/12/24/start-product.sh/" class="article-date">
  <time datetime="2018-12-24T07:01:26.000Z" itemprop="datePublished">2018-12-24</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/12/24/start-product.sh/">start-product.sh</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="start-product-sh"><a href="#start-product-sh" class="headerlink" title="start-product.sh"></a>start-product.sh</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar /opt/storybookfenxiao/fenxiao.jar --spring.profiles.active=prod</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/12/24/start-product.sh/" data-id="ckcc5iwlf0038q4uw4ouh9576" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/docker/">docker</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-start-test.sh" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/12/24/start-test.sh/" class="article-date">
  <time datetime="2018-12-24T07:01:26.000Z" itemprop="datePublished">2018-12-24</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/12/24/start-test.sh/">start-test.sh</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="start-test-sh"><a href="#start-test-sh" class="headerlink" title="start-test.sh"></a>start-test.sh</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar /opt/storybookfenxiao/fenxiao.jar --spring.profiles.active=test</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/12/24/start-test.sh/" data-id="ckcc5iwlh003bq4uwxbwz24gy" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/docker/">docker</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-容器模板" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/12/24/容器模板/" class="article-date">
  <time datetime="2018-12-24T07:01:26.000Z" itemprop="datePublished">2018-12-24</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/12/24/容器模板/">容器模板</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="容器模板"><a href="#容器模板" class="headerlink" title="容器模板"></a>容器模板</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">storybookxxx:</span><br><span class="line">  restart: always</span><br><span class="line">  ports:</span><br><span class="line">    - &apos;8077:8080&apos;</span><br><span class="line">  command:</span><br><span class="line">    - /bin/bash</span><br><span class="line">    - &apos;-c&apos;</span><br><span class="line">    - java -Xmx256m -jar /opt/storybooksale/sale.jar --spring.profiles.active=prod --config.profile=prod</span><br><span class="line">  image: &apos;registry-internal.cn-hangzhou.aliyuncs.com/com_ifenghui/storybookfenxiao:1.0.0&apos;</span><br><span class="line">  labels:</span><br><span class="line">    aliyun.scale: &apos;1&apos;</span><br><span class="line">    aliyun.lb.port_8080: http://lb-bp1uq3efysyrvt7eqa36n:8080</span><br><span class="line">  environment:</span><br><span class="line">    - constraint:aliyun.node_index==(7|8|10)</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/12/24/容器模板/" data-id="ckcc5iwlz0049q4uwdwyj81bs" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/docker/">docker</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-jvm基本操作" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/12/11/jvm基本操作/" class="article-date">
  <time datetime="2018-12-11T11:20:26.000Z" itemprop="datePublished">2018-12-11</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/12/11/jvm基本操作/">JVM基本操作</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="JVM基本操作"><a href="#JVM基本操作" class="headerlink" title="JVM基本操作"></a>JVM基本操作</h2><h2 id="1、为什么要对jvm做优化"><a href="#1、为什么要对jvm做优化" class="headerlink" title="1、为什么要对jvm做优化"></a>1、为什么要对jvm做优化</h2><p>在本地开发环境中我们很少会遇到需要对jvm进行优化的需求，但是到了生产环境，我们可能将有下面的需求：</p>
<ul>
<li>运行的应用“卡住了”，日志不输出，程序没有反应</li>
<li>服务器的CPU负载突然升高</li>
<li>在多线程应用下，如何分配线程的数量？</li>
<li>……</li>
</ul>
<p>在本次课程中，我们将对jvm有更深入的学习，我们不仅要让程序能跑起来，而且是可以跑的更快！可以分析解决在生产环境中所遇到的各种“棘手”的问题。</p>
<h2 id="2、jvm的运行参数"><a href="#2、jvm的运行参数" class="headerlink" title="2、jvm的运行参数"></a>2、jvm的运行参数</h2><p>在jvm中有很多的参数可以进行设置，这样可以让jvm在各种环境中都能够高效的运行。绝大部分的参数保持默认即可。</p>
<h3 id="2-1、三种参数类型"><a href="#2-1、三种参数类型" class="headerlink" title="2.1、三种参数类型"></a>2.1、三种参数类型</h3><p>jvm的参数类型分为三类，分别是：</p>
<ul>
<li>标准参数<ul>
<li>-help</li>
<li>-version</li>
</ul>
</li>
<li>-X参数 （非标准参数）<ul>
<li>-Xint</li>
<li>-Xcomp</li>
</ul>
</li>
<li>-XX参数（使用率较高）<ul>
<li>-XX:newSize</li>
<li>-XX:+UseSerialGC</li>
</ul>
</li>
</ul>
<h3 id="2-2、标准参数"><a href="#2-2、标准参数" class="headerlink" title="2.2、标准参数"></a>2.2、标准参数</h3><p>jvm的标准参数，一般都是很稳定的，在未来的JVM版本中不会改变，可以使用java -help检索出所有的标准参数。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">[root@node01 ~]# java -help</span><br><span class="line">用法: java [-options] class [args...]</span><br><span class="line">           (执行类)</span><br><span class="line">   或  java [-options] -jar jarfile [args...]</span><br><span class="line">           (执行 jar 文件)</span><br><span class="line">其中选项包括:</span><br><span class="line">    -d32	  使用 32 位数据模型 (如果可用)</span><br><span class="line">    -d64	  使用 64 位数据模型 (如果可用)</span><br><span class="line">    -server	  选择 "server" VM</span><br><span class="line">                  默认 VM 是 server,</span><br><span class="line">                  因为您是在服务器类计算机上运行。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    -cp &lt;目录和 zip/jar 文件的类搜索路径&gt;</span><br><span class="line">    -classpath &lt;目录和 zip/jar 文件的类搜索路径&gt;</span><br><span class="line">                  用 : 分隔的目录, JAR 档案</span><br><span class="line">                  和 ZIP 档案列表, 用于搜索类文件。</span><br><span class="line">    -D&lt;名称&gt;=&lt;值&gt;</span><br><span class="line">                  设置系统属性</span><br><span class="line">    -verbose:[class|gc|jni]</span><br><span class="line">                  启用详细输出</span><br><span class="line">    -version      输出产品版本并退出</span><br><span class="line">    -version:&lt;值&gt;</span><br><span class="line">                  警告: 此功能已过时, 将在</span><br><span class="line">                  未来发行版中删除。</span><br><span class="line">                  需要指定的版本才能运行</span><br><span class="line">    -showversion  输出产品版本并继续</span><br><span class="line">    -jre-restrict-search | -no-jre-restrict-search</span><br><span class="line">                  警告: 此功能已过时, 将在</span><br><span class="line">                  未来发行版中删除。</span><br><span class="line">                  在版本搜索中包括/排除用户专用 JRE</span><br><span class="line">    -? -help      输出此帮助消息</span><br><span class="line">    -X            输出非标准选项的帮助</span><br><span class="line">    -ea[:&lt;packagename&gt;...|:&lt;classname&gt;]</span><br><span class="line">    -enableassertions[:&lt;packagename&gt;...|:&lt;classname&gt;]</span><br><span class="line">                  按指定的粒度启用断言</span><br><span class="line">    -da[:&lt;packagename&gt;...|:&lt;classname&gt;]</span><br><span class="line">    -disableassertions[:&lt;packagename&gt;...|:&lt;classname&gt;]</span><br><span class="line">                  禁用具有指定粒度的断言</span><br><span class="line">    -esa | -enablesystemassertions</span><br><span class="line">                  启用系统断言</span><br><span class="line">    -dsa | -disablesystemassertions</span><br><span class="line">                  禁用系统断言</span><br><span class="line">    -agentlib:&lt;libname&gt;[=&lt;选项&gt;]</span><br><span class="line">                  加载本机代理库 &lt;libname&gt;, 例如 -agentlib:hprof</span><br><span class="line">                  另请参阅 -agentlib:jdwp=help 和 -agentlib:hprof=help</span><br><span class="line">    -agentpath:&lt;pathname&gt;[=&lt;选项&gt;]</span><br><span class="line">                  按完整路径名加载本机代理库</span><br><span class="line">    -javaagent:&lt;jarpath&gt;[=&lt;选项&gt;]</span><br><span class="line">                  加载 Java 编程语言代理, 请参阅 java.lang.instrument</span><br><span class="line">    -splash:&lt;imagepath&gt;</span><br><span class="line">                  使用指定的图像显示启动屏幕</span><br></pre></td></tr></table></figure>
<h4 id="2-2-1、实战"><a href="#2-2-1、实战" class="headerlink" title="2.2.1、实战"></a>2.2.1、实战</h4><blockquote>
<p><strong>实战1：查看jvm版本</strong></p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@node01 ~]# java -version</span><br><span class="line">java version "1.8.0_141"</span><br><span class="line">Java(TM) SE Runtime Environment (build 1.8.0_141-b15)</span><br><span class="line">Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> -showversion参数是表示，先打印版本信息，再执行后面的命令，在调试时非常有用，后面会使用到。</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>实战2：通过-D设置系统属性参数</strong></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestJVM</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        String str = System.getProperty(<span class="string">"str"</span>);</span><br><span class="line">        <span class="keyword">if</span> (str == <span class="keyword">null</span>) &#123;</span><br><span class="line">            System.out.println(<span class="string">"test"</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            System.out.println(str);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>进行编译、测试：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">编译</span></span><br><span class="line">[root@node01 test]# javac TestJVM.java</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">测试</span></span><br><span class="line">[root@node01 test]# java TestJVM</span><br><span class="line">test</span><br><span class="line">[root@node01 test]# java -Dstr=123 TestJVM</span><br><span class="line">123</span><br></pre></td></tr></table></figure>
<h4 id="2-2-2、-server与-client参数"><a href="#2-2-2、-server与-client参数" class="headerlink" title="2.2.2、-server与-client参数"></a>2.2.2、-server与-client参数</h4><p>可以通过-server或-client设置jvm的运行参数。</p>
<ul>
<li>它们的区别是Server VM的初始堆空间会大一些，默认使用的是并行垃圾回收器，启动慢运行快。</li>
<li>Client VM相对来讲会保守一些，初始堆空间会小一些，使用串行的垃圾回收器，它的目标是为了让JVM的启动速度更快，但运行速度会比Serverm模式慢些。</li>
<li>JVM在启动的时候会根据硬件和操作系统自动选择使用Server还是Client类型的JVM。 </li>
<li>32位操作系统<ul>
<li>如果是Windows系统，不论硬件配置如何，都默认使用Client类型的JVM。 </li>
<li>如果是其他操作系统上，机器配置有2GB以上的内存同时有2个以上CPU的话默认使用server模式，否则使用client模式。</li>
</ul>
</li>
<li>64位操作系统<ul>
<li>只有server类型，不支持client类型。</li>
</ul>
</li>
</ul>
<p>测试：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">[root@node01 test]# java -client -showversion TestJVM</span><br><span class="line">java version "1.8.0_141"</span><br><span class="line">Java(TM) SE Runtime Environment (build 1.8.0_141-b15)</span><br><span class="line">Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)</span><br><span class="line"></span><br><span class="line">test</span><br><span class="line"></span><br><span class="line">[root@node01 test]# java -server -showversion TestJVM</span><br><span class="line">java version "1.8.0_141"</span><br><span class="line">Java(TM) SE Runtime Environment (build 1.8.0_141-b15)</span><br><span class="line">Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)</span><br><span class="line"></span><br><span class="line">test</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">由于机器是64位系统，所以不支持client模式</span></span><br></pre></td></tr></table></figure>
<h3 id="2-3、-X参数"><a href="#2-3、-X参数" class="headerlink" title="2.3、-X参数"></a>2.3、-X参数</h3><p>jvm的-X参数是非标准参数，在不同版本的jvm中，参数可能会有所不同，可以通过java -X查看非标准参数。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">[root@node01 test]# java -X</span><br><span class="line">    -Xmixed           混合模式执行 (默认)</span><br><span class="line">    -Xint             仅解释模式执行</span><br><span class="line">    -Xbootclasspath:&lt;用 : 分隔的目录和 zip/jar 文件&gt;</span><br><span class="line">                      设置搜索路径以引导类和资源</span><br><span class="line">    -Xbootclasspath/a:&lt;用 : 分隔的目录和 zip/jar 文件&gt;</span><br><span class="line">                      附加在引导类路径末尾</span><br><span class="line">    -Xbootclasspath/p:&lt;用 : 分隔的目录和 zip/jar 文件&gt;</span><br><span class="line">                      置于引导类路径之前</span><br><span class="line">    -Xdiag            显示附加诊断消息</span><br><span class="line">    -Xnoclassgc       禁用类垃圾收集</span><br><span class="line">    -Xincgc           启用增量垃圾收集</span><br><span class="line">    -Xloggc:&lt;file&gt;    将 GC 状态记录在文件中 (带时间戳)</span><br><span class="line">    -Xbatch           禁用后台编译</span><br><span class="line">    -Xms&lt;size&gt;        设置初始 Java 堆大小</span><br><span class="line">    -Xmx&lt;size&gt;        设置最大 Java 堆大小</span><br><span class="line">    -Xss&lt;size&gt;        设置 Java 线程堆栈大小</span><br><span class="line">    -Xprof            输出 cpu 配置文件数据</span><br><span class="line">    -Xfuture          启用最严格的检查, 预期将来的默认值</span><br><span class="line">    -Xrs              减少 Java/VM 对操作系统信号的使用 (请参阅文档)</span><br><span class="line">    -Xcheck:jni       对 JNI 函数执行其他检查</span><br><span class="line">    -Xshare:off       不尝试使用共享类数据</span><br><span class="line">    -Xshare:auto      在可能的情况下使用共享类数据 (默认)</span><br><span class="line">    -Xshare:on        要求使用共享类数据, 否则将失败。</span><br><span class="line">    -XshowSettings    显示所有设置并继续</span><br><span class="line">    -XshowSettings:all</span><br><span class="line">                      显示所有设置并继续</span><br><span class="line">    -XshowSettings:vm 显示所有与 vm 相关的设置并继续</span><br><span class="line">    -XshowSettings:properties</span><br><span class="line">                      显示所有属性设置并继续</span><br><span class="line">    -XshowSettings:locale</span><br><span class="line">                      显示所有与区域设置相关的设置并继续</span><br><span class="line"></span><br><span class="line">-X 选项是非标准选项, 如有更改, 恕不另行通知。</span><br></pre></td></tr></table></figure>
<h4 id="2-3-1、-Xint、-Xcomp、-Xmixed"><a href="#2-3-1、-Xint、-Xcomp、-Xmixed" class="headerlink" title="2.3.1、-Xint、-Xcomp、-Xmixed"></a>2.3.1、-Xint、-Xcomp、-Xmixed</h4><ul>
<li>在解释模式(interpreted mode)下，-Xint标记会强制JVM执行所有的字节码，当然这会降低运行速度，通常低10倍或更多。</li>
<li>-Xcomp参数与它（-Xint）正好相反，JVM在第一次使用时会把所有的字节码编译成本地代码，从而带来最大程度的优化。<ul>
<li>然而，很多应用在使用-Xcomp也会有一些性能损失，当然这比使用-Xint损失的少，原因是-xcomp没有让JVM启用JIT编译器的全部功能。JIT编译器可以对是否需要编译做判断，如果所有代码都进行编译的话，对于一些只执行一次的代码就没有意义了。</li>
</ul>
</li>
<li>-Xmixed是混合模式，将解释模式与编译模式进行混合使用，由jvm自己决定，这是jvm默认的模式，也是推荐使用的模式。</li>
</ul>
<p>示例：强制设置运行模式</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">强制设置为解释模式</span></span><br><span class="line">[root@node01 test]# java  -showversion -Xint TestJVM</span><br><span class="line">java version "1.8.0_141"</span><br><span class="line">Java(TM) SE Runtime Environment (build 1.8.0_141-b15)</span><br><span class="line">Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, interpreted mode)</span><br><span class="line"></span><br><span class="line">test</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">强制设置为编译模式</span></span><br><span class="line">[root@node01 test]# java  -showversion -Xcomp TestJVM</span><br><span class="line">java version "1.8.0_141"</span><br><span class="line">Java(TM) SE Runtime Environment (build 1.8.0_141-b15)</span><br><span class="line">Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, compiled mode)</span><br><span class="line"></span><br><span class="line">test</span><br><span class="line"><span class="meta">#</span><span class="bash">注意：编译模式下，第一次执行会比解释模式下执行慢一些，注意观察。</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">默认的混合模式</span></span><br><span class="line">[root@node01 test]# java  -showversion TestJVM</span><br><span class="line">java version "1.8.0_141"</span><br><span class="line">Java(TM) SE Runtime Environment (build 1.8.0_141-b15)</span><br><span class="line">Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)</span><br><span class="line"></span><br><span class="line">test</span><br></pre></td></tr></table></figure>
<h3 id="2-4、-XX参数"><a href="#2-4、-XX参数" class="headerlink" title="2.4、-XX参数"></a>2.4、-XX参数</h3><p>-XX参数也是非标准参数，主要用于jvm的调优和debug操作。</p>
<p>-XX参数的使用有2种方式，一种是boolean类型，一种是非boolean类型：</p>
<ul>
<li>boolean类型<ul>
<li>格式：-XX:[+-]\<name> 表示启用或禁用\<name>属性</name></name></li>
<li>如：-XX:+DisableExplicitGC 表示禁用手动调用gc操作，也就是说调用System.gc()无效</li>
</ul>
</li>
<li>非boolean类型<ul>
<li>格式：-XX:\<name>=\<value>  表示\<name>属性的值为\<value></value></name></value></name></li>
<li>如：-XX:NewRatio=1 表示新生代和老年代的比值</li>
</ul>
</li>
</ul>
<p>用法：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@node01 test]# java -showversion -XX:+DisableExplicitGC TestJVM</span><br><span class="line">java version "1.8.0_141"</span><br><span class="line">Java(TM) SE Runtime Environment (build 1.8.0_141-b15)</span><br><span class="line">Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)</span><br><span class="line"></span><br><span class="line">test</span><br></pre></td></tr></table></figure>
<h3 id="2-5、-Xms与-Xmx参数"><a href="#2-5、-Xms与-Xmx参数" class="headerlink" title="2.5、-Xms与-Xmx参数"></a>2.5、-Xms与-Xmx参数</h3><p>-Xms与-Xmx分别是设置jvm的堆内存的初始大小和最大大小。</p>
<p>-Xmx2048m：等价于-XX:MaxHeapSize，设置JVM最大堆内存为2048M。</p>
<p>-Xms512m：等价于-XX:InitialHeapSize，设置JVM初始堆内存为512M。</p>
<p>适当的调整jvm的内存大小，可以充分利用服务器资源，让程序跑的更快。</p>
<p>示例：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@node01 test]# java -Xms512m -Xmx2048m TestJVM</span><br><span class="line">test</span><br></pre></td></tr></table></figure>
<h3 id="2-6、查看jvm的运行参数"><a href="#2-6、查看jvm的运行参数" class="headerlink" title="2.6、查看jvm的运行参数"></a>2.6、查看jvm的运行参数</h3><p>有些时候我们需要查看jvm的运行参数，这个需求可能会存在2种情况：</p>
<p>第一，运行java命令时打印出运行参数；</p>
<p>第二，查看正在运行的java进程的参数；</p>
<h4 id="2-6-1、运行java命令时打印参数"><a href="#2-6-1、运行java命令时打印参数" class="headerlink" title="2.6.1、运行java命令时打印参数"></a>2.6.1、运行java命令时打印参数</h4><p>运行java命令时打印参数，需要添加-XX:+PrintFlagsFinal参数即可。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">[root@node01 test]# java -XX:+PrintFlagsFinal -version</span><br><span class="line">[Global flags]</span><br><span class="line">    uintx AdaptiveSizeDecrementScaleFactor          = 4                                   &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizeMajorGCDecayTimeScale         = 10                                  &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizePausePolicy                   = 0                                   &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizePolicyCollectionCostMargin    = 50                                  &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizePolicyInitializingSteps       = 20                                  &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizePolicyOutputInterval          = 0                                   &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizePolicyWeight                  = 10                                  &#123;product&#125;</span><br><span class="line">    uintx AdaptiveSizeThroughPutPolicy              = 0                                   &#123;product&#125;</span><br><span class="line">    uintx AdaptiveTimeWeight                        = 25                                  &#123;product&#125;</span><br><span class="line">     bool AdjustConcurrency                         = false                               &#123;product&#125;</span><br><span class="line">     bool AggressiveOpts                            = false                               &#123;product&#125;</span><br><span class="line">     intx AliasLevel                                = 3                                   &#123;C2 product&#125;</span><br><span class="line">     bool AlignVector                               = true                                &#123;C2 product&#125;</span><br><span class="line">     intx AllocateInstancePrefetchLines             = 1                                   &#123;product&#125;</span><br><span class="line">     intx AllocatePrefetchDistance                  = 256                                 &#123;product&#125;</span><br><span class="line">     intx AllocatePrefetchInstr                     = 0                                   &#123;product&#125;</span><br><span class="line">     </span><br><span class="line">     …………………………略…………………………………………</span><br><span class="line">     </span><br><span class="line">    </span><br><span class="line">     bool UseXmmI2D                                 = false                               &#123;ARCH product&#125;</span><br><span class="line">     bool UseXmmI2F                                 = false                               &#123;ARCH product&#125;</span><br><span class="line">     bool UseXmmLoadAndClearUpper                   = true                                &#123;ARCH product&#125;</span><br><span class="line">     bool UseXmmRegToRegMoveAll                     = true                                &#123;ARCH product&#125;</span><br><span class="line">     bool VMThreadHintNoPreempt                     = false                               &#123;product&#125;</span><br><span class="line">     intx VMThreadPriority                          = -1                                  &#123;product&#125;</span><br><span class="line">     intx VMThreadStackSize                         = 1024                                &#123;pd product&#125;</span><br><span class="line">     intx ValueMapInitialSize                       = 11                                  &#123;C1 product&#125;</span><br><span class="line">     intx ValueMapMaxLoopSize                       = 8                                   &#123;C1 product&#125;</span><br><span class="line">     intx ValueSearchLimit                          = 1000                                &#123;C2 product&#125;</span><br><span class="line">     bool VerifyMergedCPBytecodes                   = true                                &#123;product&#125;</span><br><span class="line">     bool VerifySharedSpaces                        = false                               &#123;product&#125;</span><br><span class="line">     intx WorkAroundNPTLTimedWaitHang               = 1                                   &#123;product&#125;</span><br><span class="line">    uintx YoungGenerationSizeIncrement              = 20                                  &#123;product&#125;</span><br><span class="line">    uintx YoungGenerationSizeSupplement             = 80                                  &#123;product&#125;</span><br><span class="line">    uintx YoungGenerationSizeSupplementDecay        = 8                                   &#123;product&#125;</span><br><span class="line">    uintx YoungPLABSize                             = 4096                                &#123;product&#125;</span><br><span class="line">     bool ZeroTLAB                                  = false                               &#123;product&#125;</span><br><span class="line">     intx hashCode                                  = 5                                   &#123;product&#125;</span><br><span class="line">java version "1.8.0_141"</span><br><span class="line">Java(TM) SE Runtime Environment (build 1.8.0_141-b15)</span><br><span class="line">Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)</span><br></pre></td></tr></table></figure>
<p>由上述的信息可以看出，参数有boolean类型和数字类型，值的操作符是=或:=，分别代表默认值和被修改的值。</p>
<p>示例：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">java -XX:+PrintFlagsFinal -XX:+VerifySharedSpaces -version</span><br><span class="line"></span><br><span class="line">     intx ValueMapInitialSize                       = 11                                  &#123;C1 product&#125;</span><br><span class="line">     intx ValueMapMaxLoopSize                       = 8                                   &#123;C1 product&#125;</span><br><span class="line">     intx ValueSearchLimit                          = 1000                                &#123;C2 product&#125;</span><br><span class="line">     bool VerifyMergedCPBytecodes                   = true                                &#123;product&#125;</span><br><span class="line">     bool VerifySharedSpaces                       := true                                &#123;product&#125;</span><br><span class="line">     intx WorkAroundNPTLTimedWaitHang               = 1                                   &#123;product&#125;</span><br><span class="line">    uintx YoungGenerationSizeIncrement              = 20                                  &#123;product&#125;</span><br><span class="line">    uintx YoungGenerationSizeSupplement             = 80                                  &#123;product&#125;</span><br><span class="line">    uintx YoungGenerationSizeSupplementDecay        = 8                                   &#123;product&#125;</span><br><span class="line">    uintx YoungPLABSize                             = 4096                                &#123;product&#125;</span><br><span class="line">     bool ZeroTLAB                                  = false                               &#123;product&#125;</span><br><span class="line">     intx hashCode                                  = 5                                   &#123;product&#125;</span><br><span class="line">java version "1.8.0_141"</span><br><span class="line">Java(TM) SE Runtime Environment (build 1.8.0_141-b15)</span><br><span class="line">Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">可以看到VerifySharedSpaces这个参数已经被修改了。</span></span><br></pre></td></tr></table></figure>
<h4 id="2-6-2、查看正在运行的jvm参数"><a href="#2-6-2、查看正在运行的jvm参数" class="headerlink" title="2.6.2、查看正在运行的jvm参数"></a>2.6.2、查看正在运行的jvm参数</h4><p>如果想要查看正在运行的jvm就需要借助于jinfo命令查看。</p>
<p>首先，启动一个tomcat用于测试，来观察下运行的jvm参数。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">cd /tmp/</span><br><span class="line">rz 上传</span><br><span class="line">tar -xvf apache-tomcat-7.0.57.tar.gz </span><br><span class="line">cd apache-tomcat-7.0.57</span><br><span class="line">cd bin/</span><br><span class="line">./startup.sh</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">http://192.168.40.133:8080/ 进行访问</span></span><br></pre></td></tr></table></figure>
<p>访问成功： <img src="https://threadv.github.io/assets/1536488631901.png" alt="1536488631901"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">查看所有的参数，用法：jinfo -flags &lt;进程id&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">通过jps 或者  jps -l 查看java进程</span></span><br><span class="line">[root@node01 bin]# jps</span><br><span class="line">6346 Jps</span><br><span class="line">6219 Bootstrap</span><br><span class="line">[root@node01 bin]# jps -l</span><br><span class="line">6358 sun.tools.jps.Jps</span><br><span class="line">6219 org.apache.catalina.startup.Bootstrap</span><br><span class="line">[root@node01 bin]#</span><br><span class="line"></span><br><span class="line">[root@node01 bin]# jinfo -flags 6219</span><br><span class="line">Attaching to process ID 6219, please wait...</span><br><span class="line">Debugger attached successfully.</span><br><span class="line">Server compiler detected.</span><br><span class="line">JVM version is 25.141-b15</span><br><span class="line">Non-default VM flags: -XX:CICompilerCount=2 -XX:InitialHeapSize=31457280 -XX:MaxHeapSize=488636416 -XX:MaxNewSize=162529280 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=10485760 -XX:OldSize=20971520 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC </span><br><span class="line">Command line:  -Djava.util.logging.config.file=/tmp/apache-tomcat-7.0.57/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/tmp/apache-tomcat-7.0.57/endorsed -Dcatalina.base=/tmp/apache-tomcat-7.0.57 -Dcatalina.home=/tmp/apache-tomcat-7.0.57 -Djava.io.tmpdir=/tmp/apache-tomcat-7.0.57/temp</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">查看某一参数的值，用法：jinfo -flag &lt;参数名&gt; &lt;进程id&gt;</span></span><br><span class="line">[root@node01 bin]# jinfo -flag MaxHeapSize 6219</span><br><span class="line">-XX:MaxHeapSize=488636416</span><br></pre></td></tr></table></figure>
<h2 id="3、jvm的内存模型"><a href="#3、jvm的内存模型" class="headerlink" title="3、jvm的内存模型"></a>3、jvm的内存模型</h2><p>jvm的内存模型在1.7和1.8有较大的区别，虽然本套课程是以1.8为例进行讲解，但是我们也是需要对1.7的内存模型有所了解，所以接下里，我们将先学习1.7再学习1.8的内存模型。</p>
<h3 id="3-1、jdk1-7的堆内存模型"><a href="#3-1、jdk1-7的堆内存模型" class="headerlink" title="3.1、jdk1.7的堆内存模型"></a>3.1、jdk1.7的堆内存模型</h3><p> <img src="https://threadv.github.io/assets/1536501289198.png" alt="1536501289198"></p>
<ul>
<li><p>Young 年轻区（代）</p>
<p>Young区被划分为三部分，Eden区和两个大小严格相同的Survivor区，其中，Survivor区间中，某一时刻只有其中一个是被使用的，另外一个留做垃圾收集时复制对象用，在Eden区间变满的时候， GC就会将存活的对象移到空闲的Survivor区间中，根据JVM的策略，在经过几次垃圾收集后，任然存活于Survivor的对象将被移动到Tenured区间。</p>
</li>
<li><p>Tenured 年老区</p>
<p>Tenured区主要保存生命周期长的对象，一般是一些老的对象，当一些对象在Young复制转移一定的次数以后，对象就会被转移到Tenured区，一般如果系统中用了application级别的缓存，缓存中的对象往往会被转移到这一区间。</p>
</li>
<li><p>Perm 永久区</p>
<p>Perm代主要保存class,method,filed对象，这部份的空间一般不会溢出，除非一次性加载了很多的类，不过在涉及到热部署的应用服务器的时候，有时候会遇到java.lang.OutOfMemoryError : PermGen space 的错误，造成这个错误的很大原因就有可能是每次都重新部署，但是重新部署后，类的class没有被卸载掉，这样就造成了大量的class对象保存在了perm中，这种情况下，一般重新启动应用服务器可以解决问题。</p>
</li>
<li><p>Virtual区：</p>
<ul>
<li>最大内存和初始内存的差值，就是Virtual区。</li>
</ul>
</li>
</ul>
<h3 id="3-2、jdk1-8的堆内存模型"><a href="#3-2、jdk1-8的堆内存模型" class="headerlink" title="3.2、jdk1.8的堆内存模型"></a>3.2、jdk1.8的堆内存模型</h3><p> <img src="https://threadv.github.io/assets/1536501652150.png" alt="1536501652150"></p>
<p>由上图可以看出，jdk1.8的内存模型是由2部分组成，年轻代 + 年老代。</p>
<p>年轻代：Eden + 2*Survivor</p>
<p>年老代：OldGen</p>
<p>在jdk1.8中变化最大的Perm区，用Metaspace（元数据空间）进行了替换。</p>
<p>需要特别说明的是：Metaspace所占用的内存空间不是在虚拟机内部，而是在本地内存空间中，这也是与1.7的永久代最大的区别所在。 </p>
<p> <img src="https://threadv.github.io/assets/1536507420882.png" alt="1536507420882"></p>
<h3 id="3-3、为什么要废弃1-7中的永久区？"><a href="#3-3、为什么要废弃1-7中的永久区？" class="headerlink" title="3.3、为什么要废弃1.7中的永久区？"></a>3.3、为什么要废弃1.7中的永久区？</h3><p>官网给出了解释：<a href="http://openjdk.java.net/jeps/122" target="_blank" rel="noopener">http://openjdk.java.net/jeps/122</a></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">This is part of the JRockit and Hotspot convergence effort. JRockit customers do not need to configure the permanent generation (since JRockit does not have a permanent generation) and are accustomed to not configuring the permanent generation.</span><br><span class="line"></span><br><span class="line">移除永久代是为融合HotSpot JVM与 JRockit VM而做出的努力，因为JRockit没有永久代，不需要配置永久代。</span><br></pre></td></tr></table></figure>
<p>现实使用中，由于永久代内存经常不够用或发生内存泄露，爆出异常java.lang.OutOfMemoryError: PermGen。</p>
<p>基于此，将永久区废弃，而改用元空间，改为了使用本地内存空间。</p>
<h3 id="3-4、通过jstat命令进行查看堆内存使用情况"><a href="#3-4、通过jstat命令进行查看堆内存使用情况" class="headerlink" title="3.4、通过jstat命令进行查看堆内存使用情况"></a>3.4、通过jstat命令进行查看堆内存使用情况</h3><p>jstat命令可以查看堆内存各部分的使用量，以及加载类的数量。命令的格式如下：</p>
<p>jstat [-命令选项] [vmid] [间隔时间/毫秒] [查询次数]</p>
<h4 id="3-4-1、查看class加载统计"><a href="#3-4-1、查看class加载统计" class="headerlink" title="3.4.1、查看class加载统计"></a>3.4.1、查看class加载统计</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[root@node01 ~]# jps</span><br><span class="line">7080 Jps</span><br><span class="line">6219 Bootstrap</span><br><span class="line"></span><br><span class="line">[root@node01 ~]# jstat -class 6219</span><br><span class="line">Loaded  Bytes  Unloaded  Bytes     Time   </span><br><span class="line">  3273  7122.3        0     0.0       3.98</span><br></pre></td></tr></table></figure>
<p>说明：</p>
<ul>
<li>Loaded：加载class的数量</li>
<li>Bytes：所占用空间大小</li>
<li>Unloaded：未加载数量</li>
<li>Bytes：未加载占用空间</li>
<li>Time：时间</li>
</ul>
<h4 id="3-4-2、查看编译统计"><a href="#3-4-2、查看编译统计" class="headerlink" title="3.4.2、查看编译统计"></a>3.4.2、查看编译统计</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@node01 ~]# jstat -compiler 6219</span><br><span class="line">Compiled Failed Invalid   Time   FailedType FailedMethod</span><br><span class="line">    2376      1       0     8.04          1 org/apache/tomcat/util/IntrospectionUtils setProperty</span><br></pre></td></tr></table></figure>
<p>说明：</p>
<ul>
<li>Compiled：编译数量。</li>
<li>Failed：失败数量</li>
<li>Invalid：不可用数量</li>
<li>Time：时间</li>
<li>FailedType：失败类型</li>
<li>FailedMethod：失败的方法</li>
</ul>
<h4 id="3-4-3、垃圾回收统计"><a href="#3-4-3、垃圾回收统计" class="headerlink" title="3.4.3、垃圾回收统计"></a>3.4.3、垃圾回收统计</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">[root@node01 ~]# jstat -gc 6219</span><br><span class="line"> S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   </span><br><span class="line">9216.0 8704.0  0.0   6127.3 62976.0   3560.4   33792.0    20434.9   23808.0 23196.1 2560.0 2361.6      7    1.078   1      0.244    1.323</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">也可以指定打印的间隔和次数，每1秒中打印一次，共打印5次</span></span><br><span class="line">[root@node01 ~]# jstat -gc 6219 1000 5</span><br><span class="line"> S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   </span><br><span class="line">9216.0 8704.0  0.0   6127.3 62976.0   3917.3   33792.0    20434.9   23808.0 23196.1 2560.0 2361.6      7    1.078   1      0.244    1.323</span><br><span class="line">9216.0 8704.0  0.0   6127.3 62976.0   3917.3   33792.0    20434.9   23808.0 23196.1 2560.0 2361.6      7    1.078   1      0.244    1.323</span><br><span class="line">9216.0 8704.0  0.0   6127.3 62976.0   3917.3   33792.0    20434.9   23808.0 23196.1 2560.0 2361.6      7    1.078   1      0.244    1.323</span><br><span class="line">9216.0 8704.0  0.0   6127.3 62976.0   3917.3   33792.0    20434.9   23808.0 23196.1 2560.0 2361.6      7    1.078   1      0.244    1.323</span><br><span class="line">9216.0 8704.0  0.0   6127.3 62976.0   3917.3   33792.0    20434.9   23808.0 23196.1 2560.0 2361.6      7    1.078   1      0.244    1.323</span><br></pre></td></tr></table></figure>
<p>说明：</p>
<ul>
<li>S0C：第一个Survivor区的大小（KB）</li>
<li>S1C：第二个Survivor区的大小（KB）</li>
<li>S0U：第一个Survivor区的使用大小（KB）</li>
<li>S1U：第二个Survivor区的使用大小（KB）</li>
<li>EC：Eden区的大小（KB）</li>
<li>EU：Eden区的使用大小（KB）</li>
<li>OC：Old区大小（KB）</li>
<li>OU：Old使用大小（KB）</li>
<li>MC：方法区大小（KB）</li>
<li>MU：方法区使用大小（KB）</li>
<li>CCSC：压缩类空间大小（KB）</li>
<li>CCSU：压缩类空间使用大小（KB）</li>
<li>YGC：年轻代垃圾回收次数</li>
<li>YGCT：年轻代垃圾回收消耗时间</li>
<li>FGC：老年代垃圾回收次数</li>
<li>FGCT：老年代垃圾回收消耗时间</li>
<li>GCT：垃圾回收消耗总时间</li>
</ul>
<h2 id="4、jmap的使用以及内存溢出分析"><a href="#4、jmap的使用以及内存溢出分析" class="headerlink" title="4、jmap的使用以及内存溢出分析"></a>4、jmap的使用以及内存溢出分析</h2><p>前面通过jstat可以对jvm堆的内存进行统计分析，而jmap可以获取到更加详细的内容，如：内存使用情况的汇总、对内存溢出的定位与分析。</p>
<h3 id="4-1、查看内存使用情况"><a href="#4-1、查看内存使用情况" class="headerlink" title="4.1、查看内存使用情况"></a>4.1、查看内存使用情况</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">[root@node01 ~]# jmap -heap 6219</span><br><span class="line">Attaching to process ID 6219, please wait...</span><br><span class="line">Debugger attached successfully.</span><br><span class="line">Server compiler detected.</span><br><span class="line">JVM version is 25.141-b15</span><br><span class="line"></span><br><span class="line">using thread-local object allocation.</span><br><span class="line">Parallel GC with 2 thread(s)</span><br><span class="line"></span><br><span class="line">Heap Configuration: #堆内存配置信息</span><br><span class="line">   MinHeapFreeRatio         = 0</span><br><span class="line">   MaxHeapFreeRatio         = 100</span><br><span class="line">   MaxHeapSize              = 488636416 (466.0MB)</span><br><span class="line">   NewSize                  = 10485760 (10.0MB)</span><br><span class="line">   MaxNewSize               = 162529280 (155.0MB)</span><br><span class="line">   OldSize                  = 20971520 (20.0MB)</span><br><span class="line">   NewRatio                 = 2</span><br><span class="line">   SurvivorRatio            = 8</span><br><span class="line">   MetaspaceSize            = 21807104 (20.796875MB)</span><br><span class="line">   CompressedClassSpaceSize = 1073741824 (1024.0MB)</span><br><span class="line">   MaxMetaspaceSize         = 17592186044415 MB</span><br><span class="line">   G1HeapRegionSize         = 0 (0.0MB)</span><br><span class="line"></span><br><span class="line">Heap Usage: # 堆内存的使用情况</span><br><span class="line">PS Young Generation #年轻代</span><br><span class="line">Eden Space:</span><br><span class="line">   capacity = 123731968 (118.0MB)</span><br><span class="line">   used     = 1384736 (1.320587158203125MB)</span><br><span class="line">   free     = 122347232 (116.67941284179688MB)</span><br><span class="line">   1.1191416594941737% used</span><br><span class="line">From Space:</span><br><span class="line">   capacity = 9437184 (9.0MB)</span><br><span class="line">   used     = 0 (0.0MB)</span><br><span class="line">   free     = 9437184 (9.0MB)</span><br><span class="line">   0.0% used</span><br><span class="line">To Space:</span><br><span class="line">   capacity = 9437184 (9.0MB)</span><br><span class="line">   used     = 0 (0.0MB)</span><br><span class="line">   free     = 9437184 (9.0MB)</span><br><span class="line">   0.0% used</span><br><span class="line">PS Old Generation #年老代</span><br><span class="line">   capacity = 28311552 (27.0MB)</span><br><span class="line">   used     = 13698672 (13.064071655273438MB)</span><br><span class="line">   free     = 14612880 (13.935928344726562MB)</span><br><span class="line">   48.38545057508681% used</span><br><span class="line"></span><br><span class="line">13648 interned Strings occupying 1866368 bytes.</span><br></pre></td></tr></table></figure>
<h3 id="4-2、查看内存中对象数量及大小"><a href="#4-2、查看内存中对象数量及大小" class="headerlink" title="4.2、查看内存中对象数量及大小"></a>4.2、查看内存中对象数量及大小</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">查看所有对象，包括活跃以及非活跃的</span></span><br><span class="line">jmap -histo &lt;pid&gt; | more</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">查看活跃对象</span></span><br><span class="line">jmap -histo:live &lt;pid&gt; | more</span><br><span class="line"></span><br><span class="line">[root@node01 ~]# jmap -histo:live 6219 | more</span><br><span class="line"></span><br><span class="line"> num     #instances         #bytes  class name</span><br><span class="line">----------------------------------------------</span><br><span class="line">   1:         37437        7914608  [C</span><br><span class="line">   2:         34916         837984  java.lang.String</span><br><span class="line">   3:           884         654848  [B</span><br><span class="line">   4:         17188         550016  java.util.HashMap$Node</span><br><span class="line">   5:          3674         424968  java.lang.Class</span><br><span class="line">   6:          6322         395512  [Ljava.lang.Object;</span><br><span class="line">   7:          3738         328944  java.lang.reflect.Method</span><br><span class="line">   8:          1028         208048  [Ljava.util.HashMap$Node;</span><br><span class="line">   9:          2247         144264  [I</span><br><span class="line">  10:          4305         137760  java.util.concurrent.ConcurrentHashMap$Node</span><br><span class="line">  11:          1270         109080  [Ljava.lang.String;</span><br><span class="line">  12:            64          84128  [Ljava.util.concurrent.ConcurrentHashMap$Node;</span><br><span class="line">  13:          1714          82272  java.util.HashMap</span><br><span class="line">  14:          3285          70072  [Ljava.lang.Class;</span><br><span class="line">  15:          2888          69312  java.util.ArrayList</span><br><span class="line">  16:          3983          63728  java.lang.Object</span><br><span class="line">  17:          1271          61008  org.apache.tomcat.util.digester.CallMethodRule</span><br><span class="line">  18:          1518          60720  java.util.LinkedHashMap$Entry</span><br><span class="line">  19:          1671          53472  com.sun.org.apache.xerces.internal.xni.QName</span><br><span class="line">  20:            88          50880  [Ljava.util.WeakHashMap$Entry;</span><br><span class="line">  21:           618          49440  java.lang.reflect.Constructor</span><br><span class="line">  22:          1545          49440  java.util.Hashtable$Entry</span><br><span class="line">  23:          1027          41080  java.util.TreeMap$Entry</span><br><span class="line">  24:           846          40608  org.apache.tomcat.util.modeler.AttributeInfo</span><br><span class="line">  25:           142          38032  [S</span><br><span class="line">  26:           946          37840  java.lang.ref.SoftReference</span><br><span class="line">  27:           226          36816  [[C</span><br><span class="line">  。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。</span><br><span class="line">  </span><br><span class="line"><span class="meta">#</span><span class="bash">对象说明</span></span><br><span class="line">B  byte</span><br><span class="line">C  char</span><br><span class="line">D  double</span><br><span class="line">F  float</span><br><span class="line">I  int</span><br><span class="line">J  long</span><br><span class="line">Z  boolean</span><br><span class="line">[  数组，如[I表示int[]</span><br><span class="line">[L+类名 其他对象</span><br></pre></td></tr></table></figure>
<h3 id="4-3、将内存使用情况dump到文件中"><a href="#4-3、将内存使用情况dump到文件中" class="headerlink" title="4.3、将内存使用情况dump到文件中"></a>4.3、将内存使用情况dump到文件中</h3><p>有些时候我们需要将jvm当前内存中的情况dump到文件中，然后对它进行分析，jmap也是支持dump到文件中的。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">用法：</span></span><br><span class="line">jmap -dump:format=b,file=dumpFileName &lt;pid&gt;</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">示例</span></span><br><span class="line">jmap -dump:format=b,file=/tmp/dump.dat 6219</span><br></pre></td></tr></table></figure>
<p>  <img src="https://threadv.github.io/assets/1536512769514.png" alt="1536512769514"></p>
<p>可以看到已经在/tmp下生成了dump.dat的文件。</p>
<h3 id="4-4、通过jhat对dump文件进行分析"><a href="#4-4、通过jhat对dump文件进行分析" class="headerlink" title="4.4、通过jhat对dump文件进行分析"></a>4.4、通过jhat对dump文件进行分析</h3><p>在上一小节中，我们将jvm的内存dump到文件中，这个文件是一个二进制的文件，不方便查看，这时我们可以借助于jhat工具进行查看。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">用法：</span></span><br><span class="line">jhat -port &lt;port&gt; &lt;file&gt;</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">示例：</span></span><br><span class="line">[root@node01 tmp]# jhat -port 9999 /tmp/dump.dat </span><br><span class="line">Reading from /tmp/dump.dat...</span><br><span class="line">Dump file created Mon Sep 10 01:04:21 CST 2018</span><br><span class="line">Snapshot read, resolving...</span><br><span class="line">Resolving 204094 objects...</span><br><span class="line">Chasing references, expect 40 dots........................................</span><br><span class="line">Eliminating duplicate references........................................</span><br><span class="line">Snapshot resolved.</span><br><span class="line">Started HTTP server on port 9999</span><br><span class="line">Server is ready.</span><br></pre></td></tr></table></figure>
<p>打开浏览器进行访问：<a href="http://192.168.40.133:9999/" target="_blank" rel="noopener">http://192.168.40.133:9999/</a></p>
<p><img src="https://threadv.github.io/assets/1536513416186.png" alt="1536513416186"></p>
<p>在最后面有OQL查询功能。</p>
<p> <img src="https://threadv.github.io/assets/1536513474958.png" alt="1536513474958"></p>
<p> <img src="https://threadv.github.io/assets/1536513591638.png" alt="1536513591638"></p>
<h3 id="4-5、通过MAT工具对dump文件进行分析"><a href="#4-5、通过MAT工具对dump文件进行分析" class="headerlink" title="4.5、通过MAT工具对dump文件进行分析"></a>4.5、通过MAT工具对dump文件进行分析</h3><h4 id="4-5-1、MAT工具介绍"><a href="#4-5-1、MAT工具介绍" class="headerlink" title="4.5.1、MAT工具介绍"></a>4.5.1、MAT工具介绍</h4><p>MAT(Memory Analyzer Tool)，一个基于Eclipse的内存分析工具，是一个快速、功能丰富的JAVA heap分析工具，它可以帮助我们查找内存泄漏和减少内存消耗。使用内存分析工具从众多的对象中进行分析，快速的计算出在内存中对象的占用大小，看看是谁阻止了垃圾收集器的回收工作，并可以通过报表直观的查看到可能造成这种结果的对象。</p>
<p>官网地址：<a href="https://www.eclipse.org/mat/" target="_blank" rel="noopener">https://www.eclipse.org/mat/</a></p>
<p> <img src="https://threadv.github.io/assets/1536515331814.png" alt="1536515331814"></p>
<h4 id="4-5-2、下载安装"><a href="#4-5-2、下载安装" class="headerlink" title="4.5.2、下载安装"></a>4.5.2、下载安装</h4><p>下载地址：<a href="https://www.eclipse.org/mat/downloads.php" target="_blank" rel="noopener">https://www.eclipse.org/mat/downloads.php</a></p>
<p> <img src="https://threadv.github.io/assets/1536515395914.png" alt="1536515395914"></p>
<p>将下载得到的MemoryAnalyzer-1.8.0.20180604-win32.win32.x86_64.zip进行解压：</p>
<p> <img src="https://threadv.github.io/assets/1536515524838.png" alt="1536515524838"></p>
<h4 id="4-5-3、使用"><a href="#4-5-3、使用" class="headerlink" title="4.5.3、使用"></a>4.5.3、使用</h4><p> <img src="https://threadv.github.io/assets/1536515700264.png" alt="1536515700264"></p>
<p> <img src="https://threadv.github.io/assets/1536515764096.png" alt="1536515764096"></p>
<p> <img src="https://threadv.github.io/assets/1536515922938.png" alt="1536515922938"></p>
<p> <img src="https://threadv.github.io/assets/1536516001249.png" alt="1536516001249"></p>
<p>查看对象以及它的依赖： </p>
<p><img src="https://threadv.github.io/assets/1536516171924.png" alt="1536516171924"></p>
<p>查看可能存在内存泄露的分析： <img src="https://threadv.github.io/assets/1536516259369.png" alt="1536516259369"></p>
<h2 id="5、实战：内存溢出的定位与分析"><a href="#5、实战：内存溢出的定位与分析" class="headerlink" title="5、实战：内存溢出的定位与分析"></a>5、实战：内存溢出的定位与分析</h2><p>内存溢出在实际的生产环境中经常会遇到，比如，不断的将数据写入到一个集合中，出现了死循环，读取超大的文件等等，都可能会造成内存溢出。</p>
<p>如果出现了内存溢出，首先我们需要定位到发生内存溢出的环节，并且进行分析，是正常还是非正常情况，如果是正常的需求，就应该考虑加大内存的设置，如果是非正常需求，那么就要对代码进行修改，修复这个bug。</p>
<p>首先，我们得先学会如何定位问题，然后再进行分析。如何定位问题呢，我们需要借助于jmap与MAT工具进行定位分析。</p>
<p>接下来，我们模拟内存溢出的场景。</p>
<h3 id="5-1、模拟内存溢出"><a href="#5-1、模拟内存溢出" class="headerlink" title="5.1、模拟内存溢出"></a>5.1、模拟内存溢出</h3><p>编写代码，向List集合中添加100万个字符串，每个字符串由1000个UUID组成。如果程序能够正常执行，最后打印ok。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> cn.test.jvm;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.UUID;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestJvmOutOfMemory</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        List&lt;Object&gt; list = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10000000</span>; i++) &#123;</span><br><span class="line">            String str = <span class="string">""</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">1000</span>; j++) &#123;</span><br><span class="line">                str += UUID.randomUUID().toString();</span><br><span class="line">            &#125;</span><br><span class="line">            list.add(str);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">"ok"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>为了演示效果，我们将设置执行的参数，这里使用的是Idea编辑器。</p>
<p> <img src="https://threadv.github.io/assets/1536591244437.png" alt="1536591244437"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">参数如下：</span></span><br><span class="line">-Xms8m -Xmx8m -XX:+HeapDumpOnOutOfMemoryError</span><br></pre></td></tr></table></figure>
<h3 id="5-2、运行测试"><a href="#5-2、运行测试" class="headerlink" title="5.2、运行测试"></a>5.2、运行测试</h3><p>测试结果如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">java.lang.OutOfMemoryError: Java heap space</span><br><span class="line">Dumping heap to java_pid5348.hprof ...</span><br><span class="line">Heap dump file created [<span class="number">8137186</span> bytes in <span class="number">0.032</span> secs]</span><br><span class="line">Exception in thread <span class="string">"main"</span> java.lang.OutOfMemoryError: Java heap space</span><br><span class="line">	at java.util.Arrays.copyOf(Arrays.java:<span class="number">3332</span>)</span><br><span class="line">	at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:<span class="number">124</span>)</span><br><span class="line">	at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:<span class="number">448</span>)</span><br><span class="line">	at java.lang.StringBuilder.append(StringBuilder.java:<span class="number">136</span>)</span><br><span class="line">	at cn.test.jvm.TestJvmOutOfMemory.main(TestJvmOutOfMemory.java:<span class="number">14</span>)</span><br><span class="line"></span><br><span class="line">Process finished with exit code <span class="number">1</span></span><br></pre></td></tr></table></figure>
<p>可以看到，当发生内存溢出时，会dump文件到java_pid5348.hprof。 <img src="https://threadv.github.io/assets/1536591747657.png" alt="1536591747657"></p>
<h3 id="5-3、导入到MAT工具中进行分析"><a href="#5-3、导入到MAT工具中进行分析" class="headerlink" title="5.3、导入到MAT工具中进行分析"></a>5.3、导入到MAT工具中进行分析</h3><p><img src="https://threadv.github.io/assets/1536592135948.png" alt="1536592135948"></p>
<p>可以看到，有91.03%的内存由Object[]数组占有，所以比较可疑。</p>
<p>分析：这个可疑是正确的，因为已经有超过90%的内存都被它占有，这是非常有可能出现内存溢出的。</p>
<p>查看详情：</p>
<p><img src="https://threadv.github.io/assets/1536594010544.png" alt="1536594010544"></p>
<p>可以看到集合中存储了大量的uuid字符串。</p>
<h2 id="6、jstack的使用"><a href="#6、jstack的使用" class="headerlink" title="6、jstack的使用"></a>6、jstack的使用</h2><p>有些时候我们需要查看下jvm中的线程执行情况，比如，发现服务器的CPU的负载突然增高了、出现了死锁、死循环等，我们该如何分析呢？</p>
<p>由于程序是正常运行的，没有任何的输出，从日志方面也看不出什么问题，所以就需要看下jvm的内部线程的执行情况，然后再进行分析查找出原因。</p>
<p>这个时候，就需要借助于jstack命令了，jstack的作用是将正在运行的jvm的线程情况进行快照，并且打印出来：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">用法：jstack &lt;pid&gt;</span></span><br><span class="line"></span><br><span class="line">[root@node01 bin]# jstack 2203</span><br><span class="line">Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.141-b15 mixed mode):</span><br><span class="line"></span><br><span class="line">"Attach Listener" #24 daemon prio=9 os_prio=0 tid=0x00007fabb4001000 nid=0x906 waiting on condition [0x0000000000000000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">"http-bio-8080-exec-5" #23 daemon prio=5 os_prio=0 tid=0x00007fabb057c000 nid=0x8e1 waiting on condition [0x00007fabd05b8000]</span><br><span class="line">   java.lang.Thread.State: WAITING (parking)</span><br><span class="line">	at sun.misc.Unsafe.park(Native Method)</span><br><span class="line">	- parking to wait for  &lt;0x00000000f8508360&gt; (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)</span><br><span class="line">	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)</span><br><span class="line">	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)</span><br><span class="line">	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:104)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:32)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">"http-bio-8080-exec-4" #22 daemon prio=5 os_prio=0 tid=0x00007fab9c113800 nid=0x8e0 waiting on condition [0x00007fabd06b9000]</span><br><span class="line">   java.lang.Thread.State: WAITING (parking)</span><br><span class="line">	at sun.misc.Unsafe.park(Native Method)</span><br><span class="line">	- parking to wait for  &lt;0x00000000f8508360&gt; (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)</span><br><span class="line">	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)</span><br><span class="line">	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)</span><br><span class="line">	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:104)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:32)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">"http-bio-8080-exec-3" #21 daemon prio=5 os_prio=0 tid=0x0000000001aeb800 nid=0x8df waiting on condition [0x00007fabd09ba000]</span><br><span class="line">   java.lang.Thread.State: WAITING (parking)</span><br><span class="line">	at sun.misc.Unsafe.park(Native Method)</span><br><span class="line">	- parking to wait for  &lt;0x00000000f8508360&gt; (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)</span><br><span class="line">	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)</span><br><span class="line">	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)</span><br><span class="line">	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:104)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:32)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">"http-bio-8080-exec-2" #20 daemon prio=5 os_prio=0 tid=0x0000000001aea000 nid=0x8de waiting on condition [0x00007fabd0abb000]</span><br><span class="line">   java.lang.Thread.State: WAITING (parking)</span><br><span class="line">	at sun.misc.Unsafe.park(Native Method)</span><br><span class="line">	- parking to wait for  &lt;0x00000000f8508360&gt; (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)</span><br><span class="line">	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)</span><br><span class="line">	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)</span><br><span class="line">	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:104)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:32)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">"http-bio-8080-exec-1" #19 daemon prio=5 os_prio=0 tid=0x0000000001ae8800 nid=0x8dd waiting on condition [0x00007fabd0bbc000]</span><br><span class="line">   java.lang.Thread.State: WAITING (parking)</span><br><span class="line">	at sun.misc.Unsafe.park(Native Method)</span><br><span class="line">	- parking to wait for  &lt;0x00000000f8508360&gt; (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)</span><br><span class="line">	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)</span><br><span class="line">	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)</span><br><span class="line">	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:104)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:32)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)</span><br><span class="line">	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)</span><br><span class="line">	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">"ajp-bio-8009-AsyncTimeout" #17 daemon prio=5 os_prio=0 tid=0x00007fabe8128000 nid=0x8d0 waiting on condition [0x00007fabd0ece000]</span><br><span class="line">   java.lang.Thread.State: TIMED_WAITING (sleeping)</span><br><span class="line">	at java.lang.Thread.sleep(Native Method)</span><br><span class="line">	at org.apache.tomcat.util.net.JIoEndpoint$AsyncTimeout.run(JIoEndpoint.java:152)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">"ajp-bio-8009-Acceptor-0" #16 daemon prio=5 os_prio=0 tid=0x00007fabe82d4000 nid=0x8cf runnable [0x00007fabd0fcf000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line">	at java.net.PlainSocketImpl.socketAccept(Native Method)</span><br><span class="line">	at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)</span><br><span class="line">	at java.net.ServerSocket.implAccept(ServerSocket.java:545)</span><br><span class="line">	at java.net.ServerSocket.accept(ServerSocket.java:513)</span><br><span class="line">	at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:60)</span><br><span class="line">	at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:220)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">"http-bio-8080-AsyncTimeout" #15 daemon prio=5 os_prio=0 tid=0x00007fabe82d1800 nid=0x8ce waiting on condition [0x00007fabd10d0000]</span><br><span class="line">   java.lang.Thread.State: TIMED_WAITING (sleeping)</span><br><span class="line">	at java.lang.Thread.sleep(Native Method)</span><br><span class="line">	at org.apache.tomcat.util.net.JIoEndpoint$AsyncTimeout.run(JIoEndpoint.java:152)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">"http-bio-8080-Acceptor-0" #14 daemon prio=5 os_prio=0 tid=0x00007fabe82d0000 nid=0x8cd runnable [0x00007fabd11d1000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line">	at java.net.PlainSocketImpl.socketAccept(Native Method)</span><br><span class="line">	at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)</span><br><span class="line">	at java.net.ServerSocket.implAccept(ServerSocket.java:545)</span><br><span class="line">	at java.net.ServerSocket.accept(ServerSocket.java:513)</span><br><span class="line">	at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:60)</span><br><span class="line">	at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:220)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">"ContainerBackgroundProcessor[StandardEngine[Catalina]]" #13 daemon prio=5 os_prio=0 tid=0x00007fabe82ce000 nid=0x8cc waiting on condition [0x00007fabd12d2000]</span><br><span class="line">   java.lang.Thread.State: TIMED_WAITING (sleeping)</span><br><span class="line">	at java.lang.Thread.sleep(Native Method)</span><br><span class="line">	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1513)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">"GC Daemon" #10 daemon prio=2 os_prio=0 tid=0x00007fabe83b4000 nid=0x8b3 in Object.wait() [0x00007fabd1c2f000]</span><br><span class="line">   java.lang.Thread.State: TIMED_WAITING (on object monitor)</span><br><span class="line">	at java.lang.Object.wait(Native Method)</span><br><span class="line">	- waiting on &lt;0x00000000e315c2d0&gt; (a sun.misc.GC$LatencyLock)</span><br><span class="line">	at sun.misc.GC$Daemon.run(GC.java:117)</span><br><span class="line">	- locked &lt;0x00000000e315c2d0&gt; (a sun.misc.GC$LatencyLock)</span><br><span class="line"></span><br><span class="line">"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x00007fabe80c3800 nid=0x8a5 runnable [0x0000000000000000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">"C1 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007fabe80b6800 nid=0x8a4 waiting on condition [0x0000000000000000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007fabe80b3800 nid=0x8a3 waiting on condition [0x0000000000000000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007fabe80b2000 nid=0x8a2 runnable [0x0000000000000000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007fabe807f000 nid=0x8a1 in Object.wait() [0x00007fabd2a67000]</span><br><span class="line">   java.lang.Thread.State: WAITING (on object monitor)</span><br><span class="line">	at java.lang.Object.wait(Native Method)</span><br><span class="line">	- waiting on &lt;0x00000000e3162918&gt; (a java.lang.ref.ReferenceQueue$Lock)</span><br><span class="line">	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)</span><br><span class="line">	- locked &lt;0x00000000e3162918&gt; (a java.lang.ref.ReferenceQueue$Lock)</span><br><span class="line">	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)</span><br><span class="line">	at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)</span><br><span class="line"></span><br><span class="line">"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007fabe807a800 nid=0x8a0 in Object.wait() [0x00007fabd2b68000]</span><br><span class="line">   java.lang.Thread.State: WAITING (on object monitor)</span><br><span class="line">	at java.lang.Object.wait(Native Method)</span><br><span class="line">	- waiting on &lt;0x00000000e3162958&gt; (a java.lang.ref.Reference$Lock)</span><br><span class="line">	at java.lang.Object.wait(Object.java:502)</span><br><span class="line">	at java.lang.ref.Reference.tryHandlePending(Reference.java:191)</span><br><span class="line">	- locked &lt;0x00000000e3162958&gt; (a java.lang.ref.Reference$Lock)</span><br><span class="line">	at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)</span><br><span class="line"></span><br><span class="line">"main" #1 prio=5 os_prio=0 tid=0x00007fabe8009000 nid=0x89c runnable [0x00007fabed210000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line">	at java.net.PlainSocketImpl.socketAccept(Native Method)</span><br><span class="line">	at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)</span><br><span class="line">	at java.net.ServerSocket.implAccept(ServerSocket.java:545)</span><br><span class="line">	at java.net.ServerSocket.accept(ServerSocket.java:513)</span><br><span class="line">	at org.apache.catalina.core.StandardServer.await(StandardServer.java:453)</span><br><span class="line">	at org.apache.catalina.startup.Catalina.await(Catalina.java:777)</span><br><span class="line">	at org.apache.catalina.startup.Catalina.start(Catalina.java:723)</span><br><span class="line">	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)</span><br><span class="line">	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)</span><br><span class="line">	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)</span><br><span class="line">	at java.lang.reflect.Method.invoke(Method.java:498)</span><br><span class="line">	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:321)</span><br><span class="line">	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)</span><br><span class="line"></span><br><span class="line">"VM Thread" os_prio=0 tid=0x00007fabe8073000 nid=0x89f runnable </span><br><span class="line"></span><br><span class="line">"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007fabe801e000 nid=0x89d runnable </span><br><span class="line"></span><br><span class="line">"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007fabe8020000 nid=0x89e runnable </span><br><span class="line"></span><br><span class="line">"VM Periodic Task Thread" os_prio=0 tid=0x00007fabe80d6800 nid=0x8a6 waiting on condition </span><br><span class="line"></span><br><span class="line">JNI global references: 43</span><br></pre></td></tr></table></figure>
<h3 id="6-1、线程的状态"><a href="#6-1、线程的状态" class="headerlink" title="6.1、线程的状态"></a>6.1、线程的状态</h3><p> <img src="https://threadv.github.io/assets/1536601656694.png" alt="1536601656694"></p>
<p>在Java中线程的状态一共被分成6种：</p>
<ul>
<li><p>初始态（NEW）</p>
<ul>
<li>创建一个Thread对象，但还未调用start()启动线程时，线程处于初始态。</li>
</ul>
</li>
<li><p>运行态（RUNNABLE），在Java中，运行态包括 就绪态 和 运行态。</p>
<ul>
<li><p>就绪态</p>
<ul>
<li>该状态下的线程已经获得执行所需的所有资源，只要CPU分配执行权就能运行。</li>
<li>所有就绪态的线程存放在就绪队列中。</li>
</ul>
</li>
<li><p>运行态</p>
<ul>
<li>获得CPU执行权，正在执行的线程。</li>
<li>由于一个CPU同一时刻只能执行一条线程，因此每个CPU每个时刻只有一条运行态的线程。</li>
</ul>
</li>
</ul>
</li>
<li><p>阻塞态（BLOCKED）</p>
<ul>
<li>当一条正在执行的线程请求某一资源失败时，就会进入阻塞态。</li>
<li>而在Java中，阻塞态专指请求锁失败时进入的状态。</li>
<li>由一个阻塞队列存放所有阻塞态的线程。</li>
<li>处于阻塞态的线程会不断请求资源，一旦请求成功，就会进入就绪队列，等待执行。</li>
</ul>
</li>
<li><p>等待态（WAITING）</p>
<ul>
<li>当前线程中调用wait、join、park函数时，当前线程就会进入等待态。</li>
<li>也有一个等待队列存放所有等待态的线程。</li>
<li>线程处于等待态表示它需要等待其他线程的指示才能继续运行。</li>
<li>进入等待态的线程会释放CPU执行权，并释放资源（如：锁）</li>
</ul>
</li>
<li>超时等待态（TIMED_WAITING）<ul>
<li>当运行中的线程调用sleep(time)、wait、join、parkNanos、parkUntil时，就会进入该状态；</li>
<li>它和等待态一样，并不是因为请求不到资源，而是主动进入，并且进入后需要其他线程唤醒；</li>
<li>进入该状态后释放CPU执行权 和 占有的资源。</li>
<li>与等待态的区别：到了超时时间后自动进入阻塞队列，开始竞争锁。</li>
</ul>
</li>
<li>终止态（TERMINATED）<ul>
<li>线程执行结束后的状态。</li>
</ul>
</li>
</ul>
<h3 id="6-2、实战：死锁问题"><a href="#6-2、实战：死锁问题" class="headerlink" title="6.2、实战：死锁问题"></a>6.2、实战：死锁问题</h3><p>如果在生产环境发生了死锁，我们将看到的是部署的程序没有任何反应了，这个时候我们可以借助jstack进行分析，下面我们实战下查找死锁的原因。</p>
<h4 id="6-2-1、构造死锁"><a href="#6-2-1、构造死锁" class="headerlink" title="6.2.1、构造死锁"></a>6.2.1、构造死锁</h4><p>编写代码，启动2个线程，Thread1拿到了obj1锁，准备去拿obj2锁时，obj2已经被Thread2锁定，所以发送了死锁。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestDeadLock</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> Object obj1 = <span class="keyword">new</span> Object();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> Object obj2 = <span class="keyword">new</span> Object();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> Thread(<span class="keyword">new</span> Thread1()).start();</span><br><span class="line">        <span class="keyword">new</span> Thread(<span class="keyword">new</span> Thread2()).start();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Thread1</span> <span class="keyword">implements</span> <span class="title">Runnable</span></span>&#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">synchronized</span> (obj1)&#123;</span><br><span class="line">                System.out.println(<span class="string">"Thread1 拿到了 obj1 的锁！"</span>);</span><br><span class="line"></span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    <span class="comment">// 停顿2秒的意义在于，让Thread2线程拿到obj2的锁</span></span><br><span class="line">                    Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">                &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                <span class="keyword">synchronized</span> (obj2)&#123;</span><br><span class="line">                    System.out.println(<span class="string">"Thread1 拿到了 obj2 的锁！"</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Thread2</span> <span class="keyword">implements</span> <span class="title">Runnable</span></span>&#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">synchronized</span> (obj2)&#123;</span><br><span class="line">                System.out.println(<span class="string">"Thread2 拿到了 obj2 的锁！"</span>);</span><br><span class="line"></span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    <span class="comment">// 停顿2秒的意义在于，让Thread1线程拿到obj1的锁</span></span><br><span class="line">                    Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">                &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                <span class="keyword">synchronized</span> (obj1)&#123;</span><br><span class="line">                    System.out.println(<span class="string">"Thread2 拿到了 obj1 的锁！"</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="6-2-2、在linux上运行"><a href="#6-2-2、在linux上运行" class="headerlink" title="6.2.2、在linux上运行"></a>6.2.2、在linux上运行</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">[root@node01 test]# javac TestDeadLock.java </span><br><span class="line">[root@node01 test]# ll</span><br><span class="line">总用量 28</span><br><span class="line">-rw-r--r--. 1 root root  184 9月  11 10:39 TestDeadLock$1.class</span><br><span class="line">-rw-r--r--. 1 root root  843 9月  11 10:39 TestDeadLock.class</span><br><span class="line">-rw-r--r--. 1 root root 1567 9月  11 10:39 TestDeadLock.java</span><br><span class="line">-rw-r--r--. 1 root root 1078 9月  11 10:39 TestDeadLock$Thread1.class</span><br><span class="line">-rw-r--r--. 1 root root 1078 9月  11 10:39 TestDeadLock$Thread2.class</span><br><span class="line">-rw-r--r--. 1 root root  573 9月   9 10:21 TestJVM.class</span><br><span class="line">-rw-r--r--. 1 root root  261 9月   9 10:21 TestJVM.java</span><br><span class="line"></span><br><span class="line">[root@node01 test]# java TestDeadLock</span><br><span class="line">Thread1 拿到了 obj1 的锁！</span><br><span class="line">Thread2 拿到了 obj2 的锁！</span><br><span class="line"><span class="meta">#</span><span class="bash">这里发生了死锁，程序一直将等待下去</span></span><br></pre></td></tr></table></figure>
<h4 id="6-2-3、使用jstack进行分析"><a href="#6-2-3、使用jstack进行分析" class="headerlink" title="6.2.3、使用jstack进行分析"></a>6.2.3、使用jstack进行分析</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line">[root@node01 ~]# jstack 3256</span><br><span class="line">Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.141-b15 mixed mode):</span><br><span class="line"></span><br><span class="line">"Attach Listener" #11 daemon prio=9 os_prio=0 tid=0x00007f5bfc001000 nid=0xcff waiting on condition [0x0000000000000000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">"DestroyJavaVM" #10 prio=5 os_prio=0 tid=0x00007f5c2c008800 nid=0xcb9 waiting on condition [0x0000000000000000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">"Thread-1" #9 prio=5 os_prio=0 tid=0x00007f5c2c0e9000 nid=0xcc5 waiting for monitor entry [0x00007f5c1c7f6000]</span><br><span class="line">   java.lang.Thread.State: BLOCKED (on object monitor)</span><br><span class="line">	at TestDeadLock$Thread2.run(TestDeadLock.java:47)</span><br><span class="line">	- waiting to lock &lt;0x00000000f655dc40&gt; (a java.lang.Object)</span><br><span class="line">	- locked &lt;0x00000000f655dc50&gt; (a java.lang.Object)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">"Thread-0" #8 prio=5 os_prio=0 tid=0x00007f5c2c0e7000 nid=0xcc4 waiting for monitor entry [0x00007f5c1c8f7000]</span><br><span class="line">   java.lang.Thread.State: BLOCKED (on object monitor)</span><br><span class="line">	at TestDeadLock$Thread1.run(TestDeadLock.java:27)</span><br><span class="line">	- waiting to lock &lt;0x00000000f655dc50&gt; (a java.lang.Object)</span><br><span class="line">	- locked &lt;0x00000000f655dc40&gt; (a java.lang.Object)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x00007f5c2c0d3000 nid=0xcc2 runnable [0x0000000000000000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">"C1 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007f5c2c0b6000 nid=0xcc1 waiting on condition [0x0000000000000000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f5c2c0b3000 nid=0xcc0 waiting on condition [0x0000000000000000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007f5c2c0b1800 nid=0xcbf runnable [0x0000000000000000]</span><br><span class="line">   java.lang.Thread.State: RUNNABLE</span><br><span class="line"></span><br><span class="line">"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007f5c2c07e800 nid=0xcbe in Object.wait() [0x00007f5c1cdfc000]</span><br><span class="line">   java.lang.Thread.State: WAITING (on object monitor)</span><br><span class="line">	at java.lang.Object.wait(Native Method)</span><br><span class="line">	- waiting on &lt;0x00000000f6508ec8&gt; (a java.lang.ref.ReferenceQueue$Lock)</span><br><span class="line">	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)</span><br><span class="line">	- locked &lt;0x00000000f6508ec8&gt; (a java.lang.ref.ReferenceQueue$Lock)</span><br><span class="line">	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)</span><br><span class="line">	at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)</span><br><span class="line"></span><br><span class="line">"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007f5c2c07a000 nid=0xcbd in Object.wait() [0x00007f5c1cefd000]</span><br><span class="line">   java.lang.Thread.State: WAITING (on object monitor)</span><br><span class="line">	at java.lang.Object.wait(Native Method)</span><br><span class="line">	- waiting on &lt;0x00000000f6506b68&gt; (a java.lang.ref.Reference$Lock)</span><br><span class="line">	at java.lang.Object.wait(Object.java:502)</span><br><span class="line">	at java.lang.ref.Reference.tryHandlePending(Reference.java:191)</span><br><span class="line">	- locked &lt;0x00000000f6506b68&gt; (a java.lang.ref.Reference$Lock)</span><br><span class="line">	at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)</span><br><span class="line"></span><br><span class="line">"VM Thread" os_prio=0 tid=0x00007f5c2c072800 nid=0xcbc runnable </span><br><span class="line"></span><br><span class="line">"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f5c2c01d800 nid=0xcba runnable </span><br><span class="line"></span><br><span class="line">"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f5c2c01f800 nid=0xcbb runnable </span><br><span class="line"></span><br><span class="line">"VM Periodic Task Thread" os_prio=0 tid=0x00007f5c2c0d6800 nid=0xcc3 waiting on condition </span><br><span class="line"></span><br><span class="line">JNI global references: 6</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Found one Java-level deadlock:</span><br><span class="line">=============================</span><br><span class="line">"Thread-1":</span><br><span class="line">  waiting to lock monitor 0x00007f5c080062c8 (object 0x00000000f655dc40, a java.lang.Object),</span><br><span class="line">  which is held by "Thread-0"</span><br><span class="line">"Thread-0":</span><br><span class="line">  waiting to lock monitor 0x00007f5c08004e28 (object 0x00000000f655dc50, a java.lang.Object),</span><br><span class="line">  which is held by "Thread-1"</span><br><span class="line"></span><br><span class="line">Java stack information for the threads listed above:</span><br><span class="line">===================================================</span><br><span class="line">"Thread-1":</span><br><span class="line">	at TestDeadLock$Thread2.run(TestDeadLock.java:47)</span><br><span class="line">	- waiting to lock &lt;0x00000000f655dc40&gt; (a java.lang.Object)</span><br><span class="line">	- locked &lt;0x00000000f655dc50&gt; (a java.lang.Object)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line">"Thread-0":</span><br><span class="line">	at TestDeadLock$Thread1.run(TestDeadLock.java:27)</span><br><span class="line">	- waiting to lock &lt;0x00000000f655dc50&gt; (a java.lang.Object)</span><br><span class="line">	- locked &lt;0x00000000f655dc40&gt; (a java.lang.Object)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line"></span><br><span class="line">Found 1 deadlock.</span><br></pre></td></tr></table></figure>
<p>在输出的信息中，已经看到，发现了1个死锁，关键看这个：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">"Thread-1":</span><br><span class="line">	at TestDeadLock$Thread2.run(TestDeadLock.java:47)</span><br><span class="line">	- waiting to lock &lt;0x00000000f655dc40&gt; (a java.lang.Object)</span><br><span class="line">	- locked &lt;0x00000000f655dc50&gt; (a java.lang.Object)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br><span class="line">"Thread-0":</span><br><span class="line">	at TestDeadLock$Thread1.run(TestDeadLock.java:27)</span><br><span class="line">	- waiting to lock &lt;0x00000000f655dc50&gt; (a java.lang.Object)</span><br><span class="line">	- locked &lt;0x00000000f655dc40&gt; (a java.lang.Object)</span><br><span class="line">	at java.lang.Thread.run(Thread.java:748)</span><br></pre></td></tr></table></figure>
<p>可以清晰的看到：</p>
<ul>
<li>Thread2获取了 <0x00000000f655dc50> 的锁，等待获取 <0x00000000f655dc40> 这个锁</0x00000000f655dc40></0x00000000f655dc50></li>
<li>Thread1获取了 <0x00000000f655dc40> 的锁，等待获取 <0x00000000f655dc50> 这个锁</0x00000000f655dc50></0x00000000f655dc40></li>
<li>由此可见，发生了死锁。</li>
</ul>
<h2 id="7、VisualVM工具的使用"><a href="#7、VisualVM工具的使用" class="headerlink" title="7、VisualVM工具的使用"></a>7、VisualVM工具的使用</h2><p>VisualVM，能够监控线程，内存情况，查看方法的CPU时间和内存中的对 象，已被GC的对象，反向查看分配的堆栈(如100个String对象分别由哪几个对象分配出来的)。</p>
<p>VisualVM使用简单，几乎0配置，功能还是比较丰富的，几乎囊括了其它JDK自带命令的所有功能。</p>
<ul>
<li>内存信息</li>
<li>线程信息</li>
<li>Dump堆（本地进程）</li>
<li>Dump线程（本地进程）</li>
<li>打开堆Dump。堆Dump可以用jmap来生成。</li>
<li>打开线程Dump</li>
<li>生成应用快照（包含内存信息、线程信息等等）</li>
<li>性能分析。CPU分析（各个方法调用时间，检查哪些方法耗时多），内存分析（各类对象占用的内存，检查哪些类占用内存多）</li>
<li>……</li>
</ul>
<h3 id="7-1、启动"><a href="#7-1、启动" class="headerlink" title="7.1、启动"></a>7.1、启动</h3><p>在jdk的安装目录的bin目录下，找到jvisualvm.exe，双击打开即可。</p>
<p> <img src="https://threadv.github.io/assets/1536654243155.png" alt="1536654243155"></p>
<p><img src="https://threadv.github.io/assets/1536654347046.png" alt="1536654347046"></p>
<h3 id="7-2、查看本地进程"><a href="#7-2、查看本地进程" class="headerlink" title="7.2、查看本地进程"></a>7.2、查看本地进程</h3><p> <img src="https://threadv.github.io/assets/1536654611922.png" alt="1536654611922"></p>
<h3 id="7-3、查看CPU、内存、类、线程运行信息"><a href="#7-3、查看CPU、内存、类、线程运行信息" class="headerlink" title="7.3、查看CPU、内存、类、线程运行信息"></a>7.3、查看CPU、内存、类、线程运行信息</h3><p> <img src="https://threadv.github.io/assets/1536654708421.png" alt="1536654708421"></p>
<h3 id="7-4、查看线程详情"><a href="#7-4、查看线程详情" class="headerlink" title="7.4、查看线程详情"></a>7.4、查看线程详情</h3><p><img src="https://threadv.github.io/assets/1536654784904.png" alt="1536654784904"></p>
<p>也可以点击右上角Dump按钮，将线程的信息导出，其实就是执行的jstack命令。</p>
<p><img src="https://threadv.github.io/assets/1536656468818.png" alt="1536656468818"></p>
<p>发现，显示的内容是一样的。</p>
<h3 id="7-5、抽样器"><a href="#7-5、抽样器" class="headerlink" title="7.5、抽样器"></a>7.5、抽样器</h3><p>抽样器可以对CPU、内存在一段时间内进行抽样，以供分析。</p>
<p><img src="https://threadv.github.io/assets/1536678677028.png" alt="1536678677028"></p>
<h3 id="7-6、监控远程的jvm"><a href="#7-6、监控远程的jvm" class="headerlink" title="7.6、监控远程的jvm"></a>7.6、监控远程的jvm</h3><p>VisualJVM不仅是可以监控本地jvm进程，还可以监控远程的jvm进程，需要借助于JMX技术实现。</p>
<h4 id="7-6-1、什么是JMX？"><a href="#7-6-1、什么是JMX？" class="headerlink" title="7.6.1、什么是JMX？"></a>7.6.1、什么是JMX？</h4><p>JMX（Java Management Extensions，即Java管理扩展）是一个为应用程序、设备、系统等植入管理功能的框架。JMX可以跨越一系列异构操作系统平台、系统体系结构和网络传输协议，灵活的开发无缝集成的系统、网络和服务管理应用。</p>
<h4 id="7-6-2、监控远程的tomcat"><a href="#7-6-2、监控远程的tomcat" class="headerlink" title="7.6.2、监控远程的tomcat"></a>7.6.2、监控远程的tomcat</h4><p>想要监控远程的tomcat，就需要在远程的tomcat进行对JMX配置，方法如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">在tomcat的bin目录下，修改catalina.sh，添加如下的参数</span></span><br><span class="line"></span><br><span class="line">JAVA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">这几个参数的意思是：</span></span><br><span class="line"><span class="meta">#</span><span class="bash">-Dcom.sun.management.jmxremote ：允许使用JMX远程管理</span></span><br><span class="line"><span class="meta">#</span><span class="bash">-Dcom.sun.management.jmxremote.port=9999 ：JMX远程连接端口</span></span><br><span class="line"><span class="meta">#</span><span class="bash">-Dcom.sun.management.jmxremote.authenticate=<span class="literal">false</span> ：不进行身份认证，任何用户都可以连接</span></span><br><span class="line"><span class="meta">#</span><span class="bash">-Dcom.sun.management.jmxremote.ssl=<span class="literal">false</span> ：不使用ssl</span></span><br></pre></td></tr></table></figure>
<p>保存退出，重启tomcat。</p>
<h4 id="7-6-3、使用VisualJVM连接远程tomcat"><a href="#7-6-3、使用VisualJVM连接远程tomcat" class="headerlink" title="7.6.3、使用VisualJVM连接远程tomcat"></a>7.6.3、使用VisualJVM连接远程tomcat</h4><p>添加远程主机：</p>
<p> <img src="https://threadv.github.io/assets/1536681377182.png" alt="1536681377182"></p>
<p>在一个主机下可能会有很多的jvm需要监控，所以接下来要在该主机上添加需要监控的jvm：</p>
<p> <img src="https://threadv.github.io/assets/1536681543452.png" alt="1536681543452"></p>
<p> <img src="https://threadv.github.io/assets/1536681575620.png" alt="1536681575620"></p>
<p>连接成功。使用方法和前面就一样了，就可以和监控本地jvm进程一样，监控远程的tomcat进程。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/12/11/jvm基本操作/" data-id="ckcc5iwks002bq4uwddnxwhbx" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/jvm/">jvm</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-微信模板消息" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/12/11/微信模板消息/" class="article-date">
  <time datetime="2018-12-11T11:20:26.000Z" itemprop="datePublished">2018-12-11</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/12/11/微信模板消息/">微信模板消息</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="微信模板消息"><a href="#微信模板消息" class="headerlink" title="微信模板消息"></a>微信模板消息</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">User user = userDao.findOne(userId);</span><br><span class="line">String openid = user.getOpenId();</span><br><span class="line"></span><br><span class="line">List&lt;String&gt; stringList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">stringList.add(<span class="string">""</span>);</span><br><span class="line">stringList.add(<span class="string">"第"</span>+lastSchedule.getId()+<span class="string">"期"</span>);</span><br><span class="line">stringList.add(lastSchedule.getCardId().toString());</span><br><span class="line">String color = <span class="string">"#173177"</span>;</span><br><span class="line">String emphasisKeyword = <span class="keyword">null</span>;</span><br><span class="line">String pageStr = <span class="string">"pages/home/home"</span>;</span><br><span class="line">sendWxMsg.sendMsg(</span><br><span class="line">        openid,</span><br><span class="line">        <span class="string">"xxxxxxxxxxxxx"</span>,</span><br><span class="line">        color,</span><br><span class="line">        emphasisKeyword,</span><br><span class="line">        stringList,</span><br><span class="line">        pageStr</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sendMsg</span><span class="params">(String open_id, String templateId, String color, String emphasisKeyword, List&lt;String&gt; stringList, String pageStr)</span> </span>&#123;</span><br><span class="line">       <span class="comment">//每次手动保存一个formid针对各个用户</span></span><br><span class="line">       FormId formId = formIdService.getHasNotUseFormId(open_id);</span><br><span class="line">       <span class="keyword">if</span>(formId == <span class="keyword">null</span>) &#123;</span><br><span class="line">           <span class="keyword">return</span>;</span><br><span class="line">       &#125;</span><br><span class="line">       WxTemplate wxTemplate = <span class="keyword">new</span> WxTemplate();</span><br><span class="line">       wxTemplate.setTouser(open_id);</span><br><span class="line">       wxTemplate.setColor(color);</span><br><span class="line">       <span class="keyword">if</span>(emphasisKeyword != <span class="keyword">null</span>) &#123;</span><br><span class="line">           wxTemplate.setEmphasis_keyword(emphasisKeyword);</span><br><span class="line">       &#125;</span><br><span class="line">       wxTemplate.setForm_id(formId.getFormId());</span><br><span class="line">       wxTemplate.setPage(pageStr);</span><br><span class="line">       wxTemplate.setTemplate_id(templateId);</span><br><span class="line">       Map&lt;String, TemplateData&gt; dataMap = <span class="keyword">this</span>.getTemplateDataByStringList(stringList);</span><br><span class="line">       wxTemplate.setData(dataMap);</span><br><span class="line">       String s = JSONObject.fromObject(wxTemplate).toString();</span><br><span class="line">       String accessToken = JsTicketUtil.get_acctssToken();</span><br><span class="line">       String url = <span class="string">"https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token="</span>+accessToken;</span><br><span class="line">       JSONObject jsonObject = httpRequest(url, <span class="string">"POST"</span>, s);</span><br><span class="line">       System.out.println(jsonObject);</span><br><span class="line"></span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> Map&lt;String,TemplateData&gt; <span class="title">getTemplateDataByStringList</span><span class="params">(List&lt;String&gt; stringList)</span> </span>&#123;</span><br><span class="line">    Map&lt;String, TemplateData&gt; dataMap = <span class="keyword">new</span> HashMap&lt;String, TemplateData&gt;();</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> index = <span class="number">0</span>; index &lt; stringList.size(); index++ ) &#123;</span><br><span class="line">        TemplateData keyword = <span class="keyword">new</span> TemplateData();</span><br><span class="line">        String item = stringList.get(index);</span><br><span class="line">        <span class="keyword">int</span> name = index + <span class="number">1</span>;</span><br><span class="line">        keyword.setValue(item);</span><br><span class="line">        dataMap.put(<span class="string">"keyword"</span> + name, keyword);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> dataMap;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JsTicketUtil</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span>  String appid = <span class="string">"zzzzzz"</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span>  String appsecret = <span class="string">"zzzzzzzzzzzz"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> String server_token_url = <span class="string">"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">get_acctssToken</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        String url = server_token_url + <span class="string">"&amp;appid="</span> + appid + <span class="string">"&amp;secret="</span> + appsecret;</span><br><span class="line">        String wxjson = LoadJsonUtil.getJSON(url);</span><br><span class="line">        JSONObject json = JSONObject.fromObject(wxjson);</span><br><span class="line">        String access_token = json.getString(<span class="string">"access_token"</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> access_token;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> JSONObject <span class="title">httpRequest</span><span class="params">(String requestUrl, String requestMethod, String outputStr)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    JSONObject jsonObject = <span class="keyword">null</span>;</span><br><span class="line">    StringBuffer buffer = <span class="keyword">new</span> StringBuffer();</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="comment">// 创建SSLContext对象，并使用我们指定的信任管理器初始化</span></span><br><span class="line">        TrustManager[] tm = &#123;<span class="keyword">new</span> MyX509TrustManager()&#125;;</span><br><span class="line">        SSLContext sslContext = SSLContext.getInstance(<span class="string">"SSL"</span>, <span class="string">"SunJSSE"</span>);</span><br><span class="line">        sslContext.init(<span class="keyword">null</span>, tm, <span class="keyword">new</span> java.security.SecureRandom());</span><br><span class="line">        <span class="comment">// 从上述SSLContext对象中得到SSLSocketFactory对象</span></span><br><span class="line">        SSLSocketFactory ssf = sslContext.getSocketFactory();</span><br><span class="line">        URL url = <span class="keyword">new</span> URL(requestUrl);</span><br><span class="line">        HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();</span><br><span class="line">        httpUrlConn.setSSLSocketFactory(ssf);</span><br><span class="line">        httpUrlConn.setDoOutput(<span class="keyword">true</span>);</span><br><span class="line">        httpUrlConn.setDoInput(<span class="keyword">true</span>);</span><br><span class="line">        httpUrlConn.setUseCaches(<span class="keyword">false</span>);</span><br><span class="line">        <span class="comment">// 设置请求方式（GET/POST）</span></span><br><span class="line">        httpUrlConn.setRequestMethod(requestMethod);</span><br><span class="line">        <span class="keyword">if</span> (<span class="string">"GET"</span>.equalsIgnoreCase(requestMethod)) &#123;</span><br><span class="line">            httpUrlConn.connect();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 当有数据需要提交时</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">null</span> != outputStr) &#123;</span><br><span class="line">            OutputStream outputStream = httpUrlConn.getOutputStream();</span><br><span class="line">            <span class="comment">// 注意编码格式，防止中文乱码</span></span><br><span class="line">            outputStream.write(outputStr.getBytes(<span class="string">"UTF-8"</span>));</span><br><span class="line">            outputStream.close();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 将返回的输入流转换成字符串</span></span><br><span class="line">        InputStream inputStream = httpUrlConn.getInputStream();</span><br><span class="line">        InputStreamReader inputStreamReader = <span class="keyword">new</span> InputStreamReader(inputStream, <span class="string">"utf-8"</span>);</span><br><span class="line">        BufferedReader bufferedReader = <span class="keyword">new</span> BufferedReader(inputStreamReader);</span><br><span class="line">        String str = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">while</span> ((str = bufferedReader.readLine()) != <span class="keyword">null</span>) &#123;</span><br><span class="line">            buffer.append(str);</span><br><span class="line">        &#125;</span><br><span class="line">        bufferedReader.close();</span><br><span class="line">        inputStreamReader.close();</span><br><span class="line">        <span class="comment">// 释放资源</span></span><br><span class="line">        inputStream.close();</span><br><span class="line">        inputStream = <span class="keyword">null</span>;</span><br><span class="line">        httpUrlConn.disconnect();</span><br><span class="line">        jsonObject = JSONObject.fromObject(buffer.toString());</span><br><span class="line">    &#125; <span class="keyword">catch</span> (ConnectException ce) &#123;</span><br><span class="line">        ce.printStackTrace();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> jsonObject;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line">    <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WxTemplate</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String template_id;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String touser;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String color;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String page;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Map&lt;String,TemplateData&gt; data;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String form_id;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String emphasis_keyword;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getTemplate_id</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> template_id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTemplate_id</span><span class="params">(String template_id)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.template_id = template_id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getTouser</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> touser;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTouser</span><span class="params">(String touser)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.touser = touser;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getColor</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> color;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setColor</span><span class="params">(String color)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.color = color;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getPage</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> page;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setPage</span><span class="params">(String page)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.page = page;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Map&lt;String, TemplateData&gt; <span class="title">getData</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> data;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setData</span><span class="params">(Map&lt;String, TemplateData&gt; data)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.data = data;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getForm_id</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> form_id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setForm_id</span><span class="params">(String form_id)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.form_id = form_id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getEmphasis_keyword</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> emphasis_keyword;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setEmphasis_keyword</span><span class="params">(String emphasis_keyword)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.emphasis_keyword = emphasis_keyword;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/12/11/微信模板消息/" data-id="ckcc5iwm1004cq4uw7842xkwj" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/wechat/">wechat</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-JDK1.8 LocalDate" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/12/11/JDK1.8 LocalDate/" class="article-date">
  <time datetime="2018-12-11T10:53:35.000Z" itemprop="datePublished">2018-12-11</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/12/11/JDK1.8 LocalDate/">JDK1.8 LocalDate</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="LocalDateTime"><a href="#LocalDateTime" class="headerlink" title="LocalDateTime"></a>LocalDateTime</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">表示日期的LocalDate </span><br><span class="line">表示时间的LocalTime </span><br><span class="line">表示日期时间的LocalDateTime</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 从默认时区的系统时钟获取当前的日期时间。不用考虑时区差</span></span><br><span class="line">LocalDateTime date = LocalDateTime.now();</span><br><span class="line"><span class="comment">//2018-07-15T14:22:39.759</span></span><br><span class="line">System.out.println(date);</span><br><span class="line"></span><br><span class="line">System.out.println(date.getYear());</span><br><span class="line">System.out.println(date.getMonthValue());</span><br><span class="line">System.out.println(date.getDayOfMonth());</span><br><span class="line">System.out.println(date.getHour());</span><br><span class="line">System.out.println(date.getMinute());</span><br><span class="line">System.out.println(date.getSecond());</span><br><span class="line">System.out.println(date.getNano());</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 手动创建一个LocalDateTime实例</span></span><br><span class="line">LocalDateTime date2 = LocalDateTime.of(<span class="number">2017</span>, <span class="number">12</span>, <span class="number">17</span>, <span class="number">9</span>, <span class="number">31</span>, <span class="number">31</span>, <span class="number">31</span>);</span><br><span class="line">System.out.println(date2);</span><br><span class="line"><span class="comment">// 进行加操作，得到新的日期实例</span></span><br><span class="line">LocalDateTime date3 = date2.plusDays(<span class="number">12</span>);</span><br><span class="line">System.out.println(date3);</span><br><span class="line"><span class="comment">// 进行减操作，得到新的日期实例</span></span><br><span class="line">LocalDateTime date4 = date3.minusYears(<span class="number">2</span>);</span><br><span class="line">System.out.println(date4);</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 时间戳  1970年1月1日00：00：00 到某一个时间点的毫秒值</span></span><br><span class="line"><span class="comment">// 默认获取UTC时区</span></span><br><span class="line">Instant ins = Instant.now();</span><br><span class="line">System.out.println(ins);</span><br><span class="line"></span><br><span class="line">System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of(<span class="string">"+8"</span>)).toEpochMilli());</span><br><span class="line">System.out.println(System.currentTimeMillis());</span><br><span class="line"></span><br><span class="line">System.out.println(Instant.now().toEpochMilli());</span><br><span class="line">System.out.println(Instant.now().atOffset(ZoneOffset.ofHours(<span class="number">8</span>)).toInstant().toEpochMilli());</span><br></pre></td></tr></table></figure>
<ul>
<li>计算时间间隔</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// Duration:计算两个时间之间的间隔</span></span><br><span class="line"><span class="comment">// Period：计算两个日期之间的间隔</span></span><br><span class="line"></span><br><span class="line">Instant ins1 = Instant.now();</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">&#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">    e.printStackTrace();</span><br><span class="line">&#125;</span><br><span class="line">Instant ins2 = Instant.now();</span><br><span class="line">Duration dura = Duration.between(ins1, ins2);</span><br><span class="line">System.out.println(dura);</span><br><span class="line">System.out.println(dura.toMillis());</span><br><span class="line"></span><br><span class="line">System.out.println(<span class="string">"======================"</span>);</span><br><span class="line">LocalTime localTime = LocalTime.now();</span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">&#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">    e.printStackTrace();</span><br><span class="line">&#125;</span><br><span class="line">LocalTime localTime2 = LocalTime.now();</span><br><span class="line">Duration du2 = Duration.between(localTime, localTime2);</span><br><span class="line">System.out.println(du2);</span><br><span class="line">System.out.println(du2.toMillis());</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">LocalDate localDate =LocalDate.now();</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">&#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">    e.printStackTrace();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">LocalDate localDate2 = LocalDate.of(<span class="number">2019</span>,<span class="number">12</span>,<span class="number">12</span>);</span><br><span class="line">Period pe = Period.between(localDate, localDate2);</span><br><span class="line">System.out.println(pe.getYears());</span><br><span class="line">System.out.println(pe);</span><br></pre></td></tr></table></figure>
<ul>
<li>获取特定天数</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// temperalAdjust 时间校验器</span></span><br><span class="line"><span class="comment">// 例如获取下周日  下一个工作日</span></span><br><span class="line">LocalDateTime ldt1 = LocalDateTime.now();</span><br><span class="line">System.out.println(ldt1);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取一年中的第一天</span></span><br><span class="line">LocalDateTime ldt2 = ldt1.withDayOfYear(<span class="number">1</span>);</span><br><span class="line">System.out.println(ldt2);</span><br><span class="line"><span class="comment">// 获取一个月中的第一天</span></span><br><span class="line">LocalDateTime ldt3 = ldt1.withDayOfMonth(<span class="number">1</span>);</span><br><span class="line">System.out.println(ldt3);</span><br><span class="line"></span><br><span class="line">LocalDateTime ldt4 = ldt1.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));</span><br><span class="line">System.out.println(ldt4);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取下一个工作日</span></span><br><span class="line">LocalDateTime ldt5 = ldt1.with((t) -&gt; &#123;</span><br><span class="line">    LocalDateTime ldt6 = (LocalDateTime)t;</span><br><span class="line">    DayOfWeek dayOfWeek = ldt6.getDayOfWeek();</span><br><span class="line">    <span class="keyword">if</span> (DayOfWeek.FRIDAY.equals(dayOfWeek))&#123;</span><br><span class="line">        <span class="keyword">return</span> ldt6.plusDays(<span class="number">3</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (DayOfWeek.SATURDAY.equals(dayOfWeek))&#123;</span><br><span class="line">        <span class="keyword">return</span> ldt6.plusDays(<span class="number">2</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> ldt6.plusDays(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">System.out.println(ldt5);</span><br></pre></td></tr></table></figure>
<ul>
<li>格式化时间/日期</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// DateTimeFormatter: 格式化时间/日期</span></span><br><span class="line"><span class="comment">// 自定义格式</span></span><br><span class="line">LocalDateTime ldt = LocalDateTime.now();</span><br><span class="line">DateTimeFormatter formatter = DateTimeFormatter.ofPattern(<span class="string">"yyyy年MM月dd日"</span>);</span><br><span class="line"><span class="comment">//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");</span></span><br><span class="line">String strDate1 = ldt.format(formatter);</span><br><span class="line">String strDate = formatter.format(ldt);</span><br><span class="line">System.out.println(strDate);</span><br><span class="line">System.out.println(strDate1);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用api提供的格式</span></span><br><span class="line">DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;</span><br><span class="line">LocalDateTime ldt2 = LocalDateTime.now();</span><br><span class="line">String strDate3 = dtf.format(ldt2);</span><br><span class="line">System.out.println(strDate3);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 解析字符串to时间</span></span><br><span class="line">DateTimeFormatter df = DateTimeFormatter.ofPattern(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>);</span><br><span class="line">LocalDateTime time = LocalDateTime.now();</span><br><span class="line">String localTime = df.format(time);</span><br><span class="line">LocalDateTime ldt4 = LocalDateTime.parse(<span class="string">"2017-09-28 17:07:05"</span>,df);</span><br><span class="line">System.out.println(<span class="string">"LocalDateTime转成String类型的时间："</span>+localTime);</span><br><span class="line">System.out.println(<span class="string">"String类型的时间转成LocalDateTime："</span>+ldt4);</span><br></pre></td></tr></table></figure>
<hr>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//获取当前日期,只含年月日 固定格式 yyyy-MM-dd    2018-21-11</span></span><br><span class="line">LocalDate today = LocalDate.now();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 根据年月日取日期，6月就是6，</span></span><br><span class="line">LocalDate oldDate = LocalDate.of(<span class="number">2018</span>, <span class="number">6</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 根据字符串取：默认格式yyyy-MM-dd，02不能写成2</span></span><br><span class="line">LocalDate yesteday = LocalDate.parse(<span class="string">"2018-06-03"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果不是闰年 传入29号也会报错</span></span><br><span class="line">LocalDate.parse(<span class="string">"2018-02-29"</span>);</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 日期转换常用,第一天或者最后一天...</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">localDateTransferTest</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">//2018-05-04</span></span><br><span class="line">    LocalDate today = LocalDate.now();</span><br><span class="line">    <span class="comment">// 取本月第1天： 2018-05-01</span></span><br><span class="line">    LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());</span><br><span class="line">    <span class="comment">// 取本月第2天：2018-05-02</span></span><br><span class="line">    LocalDate secondDayOfThisMonth = today.withDayOfMonth(<span class="number">2</span>);</span><br><span class="line">    <span class="comment">// 取本月最后一天，再也不用计算是28，29，30还是31： 2018-05-31</span></span><br><span class="line">    LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth());</span><br><span class="line">    <span class="comment">// 取下一天：2018-06-01</span></span><br><span class="line">    LocalDate firstDayOf2015 = lastDayOfThisMonth.plusDays(<span class="number">1</span>);</span><br><span class="line">    <span class="comment">// 取2018年10月第一个周三 so easy?：  2018-10-03</span></span><br><span class="line">    LocalDate thirdMondayOf2018 = LocalDate.parse(<span class="string">"2018-10-01"</span>).with(TemporalAdjusters.firstInMonth(DayOfWeek.WEDNESDAY));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">localTimeTest</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">//16:25:46.448(纳秒值)</span></span><br><span class="line">    LocalTime todayTimeWithMillisTime = LocalTime.now();</span><br><span class="line">    <span class="comment">//16:28:48 不带纳秒值</span></span><br><span class="line">    LocalTime todayTimeWithNoMillisTime = LocalTime.now().withNano(<span class="number">0</span>);</span><br><span class="line">    LocalTime time1 = LocalTime.parse(<span class="string">"23:59:59"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">localDateTimeTest</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">//转化为时间戳  毫秒值</span></span><br><span class="line">    <span class="keyword">long</span> time1 = LocalDateTime.now().toInstant(ZoneOffset.of(<span class="string">"+8"</span>)).toEpochMilli();</span><br><span class="line">    <span class="keyword">long</span> time2 = System.currentTimeMillis();</span><br><span class="line"></span><br><span class="line">    <span class="comment">//时间戳转化为localdatetime</span></span><br><span class="line">    DateTimeFormatter df= DateTimeFormatter.ofPattern(<span class="string">"YYYY-MM-dd HH:mm:ss.SSS"</span>);</span><br><span class="line"></span><br><span class="line">    System.out.println(df.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time1),ZoneId.of(<span class="string">"Asia/Shanghai"</span>))));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/12/11/JDK1.8 LocalDate/" data-id="ckcc5iwih0008q4uwccil3y6z" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-nginx配置重启" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/12/11/nginx配置重启/" class="article-date">
  <time datetime="2018-12-11T09:09:11.000Z" itemprop="datePublished">2018-12-11</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/12/11/nginx配置重启/">nginx 重启配置</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="nginx-重启配置"><a href="#nginx-重启配置" class="headerlink" title="nginx 重启配置"></a>nginx 重启配置</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"> /etc/init.d/nginx configtest</span><br><span class="line"> /etc/init.d/nginx reload</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">systemctl reload nginx</span><br></pre></td></tr></table></figure>
<ul>
<li>查看CentOS版本方法  <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lsb_release -a</span><br></pre></td></tr></table></figure></li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/12/11/nginx配置重启/" data-id="ckcc5iwl3002qq4uwd8agbn1x" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/linux/">linux</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-weixin4j" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/12/10/weixin4j/" class="article-date">
  <time datetime="2018-12-10T03:37:26.000Z" itemprop="datePublished">2018-12-10</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2018/12/10/weixin4j/">weixin4j</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h5 id="weixin4j"><a href="#weixin4j" class="headerlink" title="weixin4j"></a>weixin4j</h5><p><a href="https://blog.csdn.net/yakson/article/details/82108649" target="_blank" rel="noopener">https://blog.csdn.net/yakson/article/details/82108649</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/12/10/weixin4j/" data-id="ckcc5iwlq003uq4uww3a72s1o" class="article-share-link">Teilen</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/wechat/">wechat</a></li></ul>

    </footer>
  </div>
  
</article>


  


  <nav id="page-nav">
    
    <a class="extend prev" rel="prev" href="/page/4/">&laquo; zurück</a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/3/">3</a><a class="page-number" href="/page/4/">4</a><span class="page-number current">5</span><a class="page-number" href="/page/6/">6</a><a class="page-number" href="/page/7/">7</a><span class="space">&hellip;</span><a class="page-number" href="/page/15/">15</a><a class="extend next" rel="next" href="/page/6/">weiter &raquo;</a>
  </nav>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Design-Patterns/">Design Patterns</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Git/">Git</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Html/">Html</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Http/">Http</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JavaScript/">JavaScript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Sort/">Sort</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/algorithm/">algorithm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ali-weixin-pay/">ali/weixin pay</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/">docker</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/elasticsearch/">elasticsearch</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/go/">go</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/idea/">idea</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/">java</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/jvm/">jvm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/logstash/">logstash</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mysql/">mysql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/php/">php</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/pytest/">pytest</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/springboot/">springboot</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/util/">util</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vue/">vue</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/wechat/">wechat</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/我的世界/">我的世界</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/消息队列/">消息队列</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/线程/">线程</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Design-Patterns/" style="font-size: 16.36px;">Design Patterns</a> <a href="/tags/Git/" style="font-size: 10px;">Git</a> <a href="/tags/Html/" style="font-size: 10px;">Html</a> <a href="/tags/Http/" style="font-size: 12.73px;">Http</a> <a href="/tags/JavaScript/" style="font-size: 15.45px;">JavaScript</a> <a href="/tags/Sort/" style="font-size: 18.18px;">Sort</a> <a href="/tags/algorithm/" style="font-size: 11.82px;">algorithm</a> <a href="/tags/ali-weixin-pay/" style="font-size: 13.64px;">ali/weixin pay</a> <a href="/tags/docker/" style="font-size: 17.27px;">docker</a> <a href="/tags/elasticsearch/" style="font-size: 10px;">elasticsearch</a> <a href="/tags/go/" style="font-size: 11.82px;">go</a> <a href="/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/tags/idea/" style="font-size: 10px;">idea</a> <a href="/tags/java/" style="font-size: 20px;">java</a> <a href="/tags/jvm/" style="font-size: 11.82px;">jvm</a> <a href="/tags/linux/" style="font-size: 18.18px;">linux</a> <a href="/tags/logstash/" style="font-size: 12.73px;">logstash</a> <a href="/tags/mysql/" style="font-size: 14.55px;">mysql</a> <a href="/tags/php/" style="font-size: 11.82px;">php</a> <a href="/tags/pytest/" style="font-size: 10px;">pytest</a> <a href="/tags/python/" style="font-size: 13.64px;">python</a> <a href="/tags/springboot/" style="font-size: 17.27px;">springboot</a> <a href="/tags/util/" style="font-size: 19.09px;">util</a> <a href="/tags/vue/" style="font-size: 10px;">vue</a> <a href="/tags/wechat/" style="font-size: 10.91px;">wechat</a> <a href="/tags/我的世界/" style="font-size: 10px;">我的世界</a> <a href="/tags/消息队列/" style="font-size: 10.91px;">消息队列</a> <a href="/tags/线程/" style="font-size: 11.82px;">线程</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archiv</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/05/">May 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/04/">April 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/03/">March 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/01/">January 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">October 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">June 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">April 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">March 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/02/">February 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/01/">January 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/12/">December 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/11/">November 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">October 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/09/">September 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/07/">July 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/06/">June 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">May 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/04/">April 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">March 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">February 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">January 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">December 2017</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">letzter Beitrag</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/05/17/linux/linux_shell/">linux—shell</a>
          </li>
        
          <li>
            <a href="/2020/05/16/linux/linux_awk_sed/">linux-awk\sed 示例</a>
          </li>
        
          <li>
            <a href="/2020/04/11/linux/linux_bash/">linux—bash</a>
          </li>
        
          <li>
            <a href="/2020/03/29/hello-world/">Hello World</a>
          </li>
        
          <li>
            <a href="/2020/03/16/go基础/">go基础知识</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 vchen<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



  </div>
</body>
</html>